//选择排序算法 voidsort(intlist[], int n) { for (int i = 0; i < n - 1; i++) { // int min = i; for (int j = i + 1; j < n;j++) if (list[j] < list[i]) // min = j; select_swap(list+j, list+i); } }
voidselect_swap(int *a, int *b) { int temp; temp = *a; *a = *b; *b = temp; }
//测试主函数 #include<stdio.h> #include<stdlib.h> #include<math.h> #define MAX_SIZE 100 voidselect_swap(int *a, int *b); voidsort(intlist[], int n); intmain() { int num; intlist[MAX_SIZE]; printf("Enter the number of numbers to generate:"); scanf_s("%d", &num); if (num<1 || num>MAX_SIZE) { fprintf(stderr, "Improper value of n.\n"); exit(1); } for (int i = 0; i < num; i++) { list[i] = rand() % 1000; printf("%d ", list[i]); } sort(list, num); printf("\nsorted array:\n"); for (int i = 0; i < num; i++) printf("%d ", list[i]); printf("\n"); system("pause"); return0; }