描述:从向量头部开始,找出第一小的放到前面,第二小的放到第二个位置。。。。。。
template <typename T>
void selectionSort(vector<T>& v)
{
// index of smallest item in each pass
int smallIndex;
// save the vector size in n
int pass, j, n = v.size();
T temp;
// sort v[0]..v[n-2], and arr[n-1] is in place
for (pass = 0; pass < n-1; pass++)
{
// start the scan at index pass; set smallIndex to pass
smallIndex = pass;
// j scans the sublist v[pass+1]..v[n-1] 找出从当前位置开始的最小的元素
for (j = pass+1; j < n; j++)
// update smallIndex if smaller element is found
if (v[j] < v[smallIndex])
smallIndex = j;
// when finished, place smallest item in arr[pass]
if (smallIndex != pass) 放到当前位置
{
temp = v[pass];
v[pass] = v[smallIndex];
v[smallIndex] = temp;
}
}
}