#include<iostream>
using namespace std;
//选择排序
void test02()
{
int a[] = { 4,5,8,6,5,1,11 };
int len = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < len - 1; i++)
{
int min = a[i];
int minpos = i;
for (int j =i+1 ; j < len ; j++)
{
if (min > a[j])
{
min = a[j];
minpos = j;
}
}
if (minpos != i)
{
int tmp = a[i];
a[i] = a[minpos];
a[minpos] = tmp;
}
}
for (int iter : a)
{
cout << iter << " "; //1 4 5 5 6 8 11
}
}
int main()
{
test02();
system("pause");
return 0;
}
c++ 选择排序
最新推荐文章于 2024-08-21 18:16:36 发布