選擇法排序
阿新 • • 發佈:2018-12-18
#include<stdio.h> #include<stdlib.h> #include<string.h>
int main(void){
int a[] = { 1, 3, 4, 22, 9, 8 }; int n; int j = 0; int tmp = 0; n = sizeof(a) / sizeof(a[0]); //6 printf("排序前\n"); for (int i = 0; i < n; i++){ printf("%d ", a[i]); } printf("\n");
for (int i = 0; i < n-1; i++){ for (int j = i+1; j < n; j++){ if (a[i] > a[j]){ tmp = a[i]; a[i] = a[j]; a[j] = tmp;
} }
}
printf("排序後\n"); for (int i = 0; i < n; i++){ printf("%d ", a[i]); } printf("\n"); system("pause"); return 0;
}