为什么这种排序不起作用? (C)

Why does this sorting doesn't work ? (C)

我正在尝试创建一个基于选择排序算法的排序方法 使用当前代码,数组 [10, 9, 8 .. 1] 是 "sorted" 到 [9, 8 .. 2, 10, 1] 我的意思是,它甚至没有把 10 放在正确的位置

10 9 8 7 6 5 4 3 2 1

"sorted" 至

9 8 7 6 5 4 3 2 10 1

有什么问题吗?

void selectionSort(int array[], int length)
{
    int i = 0, j = 0, temp = 0, swap = 0;
    for(i = 0; i < length; i++)
    {
        temp = i;
        for(j = 0; j < length; j++)
        {
            if(array[temp] > array[j])
            {
                temp = j;
            }
        }
        swap = array[temp];
        array[temp] = array[i];
        array[i] = swap;
    }
}

在 i 的每次迭代之后,应该对直到 i 的数组进行排序。您可以在 i 的每次迭代后打印数组,并可以看到逻辑错误。

内循环应该这样写

for(j = i + 1; j < length; j++)
    ^^^^^^^^^