#include<iostream>
using namespace std;
void selcet(int arry[]);
int length;
int main() {
int a[] = { 1,-17,13,15,-19,10,99,123,124,1000 };
length = sizeof(a) / sizeof(int);
for (int i = 0; i < length; i++)
{
cout << a[i] << " ";
}
cout << endl << length << "个" << endl;
selcet(a);
return 0;
}
void selcet(int a[]) {
int num , max,i,n;
for ( i = 0; i < length-1; i++)
{
num = i;
for (n = i+1; n < length; n++)
{
if (a[num] < a[n]) {
num = n;
}
}
max = a[i];
a[i] = a[num];
a[num] = max;
}
for (int i = 0; i < length; i++)
{
cout << a[i] << " ";
}
}
完整可运行,
该算法核心:遍历找出最大数值所在的位子与第一个交换,然后从第二个开始重复此操作,以此类推 。