你们可以解释/模拟选择排序算法,我倾向于在值的交换部分丢失。谢谢!
这是代码:
int[] ars = new int[4] { 5, 3, 10, 6 };
int min, tempo;
for (int i = 0; i < ars.Length -1; i++)
{
min = i;
for(int ii = i + 1; ii < ars.Length; ii++)
if (ars[ii] < ars[min])
{
min = ii;
}
tempo = ars[min];
ars[min] = ars[i];
ars[i] = tempo;
}
答案 0 :(得分:0)
它将迭代列表并交换当前项目,其中列表的其余部分找到最低值,但仅当它低于当前项目时。
首先,您需要在复制值之前“记住”旧值。
// create copy of the previous value
tempo = ars[min];
// overwrite the old value.
ars[min] = ars[i];
// recall the previous value and assign it to the other in the array
ars[i] = tempo;