运行截图:
下面是完整代码:
#include <iostream>
#include <stdio.h>
using namespace std;
//直接选择排序:两两比较,直至完全有序
//正序
void xp(int a[],int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
int main() {
int a[] = { 3,7,5,3,9,0,1,8,4 };
int len = sizeof(a) / sizeof(a[0]);
for (int i = 0; i < len; i++) {
cout << a[i] << " ";
}
cout << endl;
xp(a, len);
for (int i = 0; i < len; i++) {
cout << a[i] << " ";
}
return 0;
}