選擇排序_C語言_數組。本站提示廣大學習愛好者:(選擇排序_C語言_數組)文章只能為提供參考,不一定能成為您想要的結果。以下是選擇排序_C語言_數組正文
#include <stdio.h> void select_sort(int *); int main(int argc, const char * argv[]) { //初始化數組 int a[10] = {1, 6, 8, 9, 3, 2, 4, 5, 7, 0}; //亂序 printf("亂序\n"); for (int i = 0; i < 10; i ++ ) { printf("%d ",a[i]); } printf("\n\n"); select_sort(a); //順序 printf("順序\n"); for (int i = 0; i < 10; i ++ ) { printf("%d ",a[i]); } printf("\n\n"); return 0; } void select_sort(int * a) { int i = 0; int j; int k; int temp; for (i; i < 10; i ++) { temp = a[i]; k = i; for (j = i + 1; j < 10; j++) { if (temp > a[j]) { temp = a[j]; k = j; } } if (k != i) { temp = a[i]; a[i] = a[k]; a[k] = temp; } } }