选择排序优点:交换次数少
void SelectSort(int* array, int len)
{
if (array==NULL||len<=1)
{
return;
}
int tmp;
int index;
for (auto i = 0; i < len;++i)
{
tmp = array[i];
index = i;
for (auto j = i; j < len;++j)
{
if (tmp>array[j])
{
tmp = array[j];
index = j;
}
}
std::swap(array[i],array[index]);
}
}
int main()
{
int a[4]{3, 2, 1, 5};
SelectSort(a, 4);
return 0;
}