习题 5.2 用选择法对10个整数排序。
代码:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,min,min_ ;
int arr[10];
cout << "请输入10个整数:" << endl;
for (i = 0; i != 10 ;++i)
{
cin >> arr[i];
}
for (i = 0; i != 10; ++i)
{
min = arr[i];
min_ = i;
for (j = i+1; j != 10; ++j)
{
if (arr[j] < min)
{
min = arr[j];
min_ = j; //存储最小值下标
}
}
arr[min_] = arr[i];
arr[i] = min;
}
cout << "按从小到大的顺序:" << endl;
for(i = 0; i != 10; ++i )
{
cout <<setw(4) << arr[i];
}
return 0;
}