//選擇排序初步理解
0,循環n趟。下面1,2是循環體:
1,從沒有排序中找出最小值 。 minIndex = i;//待交換的索引:也就是從List[i+1]--List[n-1]處找到最小值索引,並賦值給minIndex
2, 將當前待交換的數(nIndex=i)與min交換
void selcetSort(){
int a[6] = { 3, 2, 1, 6, 5, 4 };
for (int i = 0; i < 6; i++)// 1,循環n趟。
{
//2,在沒有排序中的數組中找最小值
int minIndex = i;//待交換的縮影
for (int j= i+1; j < 6; j++)//2.1在沒有排序中的數組中找最小值 範圍爲[i+1,N]
{
if (a[minIndex] > a[j])
{
minIndex = j;
}
}
////每一趟找出的最小值 與當前值進行交換 數組交換
swap(a[minIndex], a[i]);
}
}