选择排序 -
首先初始化最小元素索引值为首元素,依次遍历待排序数列,若遇到小于该最小索引位置处的元素则刷新最小索引为该较小元素的位置,直至遇到尾元素,结束一次遍历,并将最小索引处元素与首元素交换;然后,初始化最小索引值为第二个待排序数列元素位置,同样的操作,可得到数列第二个元素即为次小元素;以此类推。
时间复杂度:O(N2) 稳定性:不稳定
/*选择排序*/
void selectSort(vector<int> &arr, int bgn, int end)
{
for (int i = bgn; i < end; ++i)
{
int minIndex = i;
for (int j = i + 1; j < end; ++j)
{
if (arr[j] < arr[minIndex])
minIndex = j;
}
if (minIndex != i)
mySwap(&arr[i], &arr[minIndex]);
}
}