#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:查找次數(shù)一樣減少交換次數(shù)
#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();
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者