#include <stdio.h>
void main(){
int arr[5] = { 2, 323, 3, 3233, 32 };
int tmp;
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 5; j++) {
if (arr[i] > arr[j]) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
for (int i = 0; i < 5; i++) {
printf("%d\n", arr[i]);
}
getchar();
}
2:查找次数一样减少交换次数
#include <stdio.h>
void main(){
int arr[5] = { 2, 323, 3, 3233, 32 };
int tmp;
int idx;
for (int i = 0; i < 4; i++) {
idx = i;
for (int j = i + 1; j < 5; j++) {
if (arr[idx] > arr[j]) {
idx = j;
}
}
if (idx != i) {
tmp = arr[i];
arr[i] = arr[idx];
arr[idx] = tmp;
}
}
for (int i = 0; i < 5; i++) {
printf("%d\n", arr[i]);
}
getchar();
}