# Include <stdio. h> # include <assert. h> void display (int * a, int n) {assert (a); for (int I = 0; I <n; I ++) {printf ("% d, ", a [I]);} printf (" \ n ");} void swap (int * a, int * B) {assert (a); assert (B ); int temp; temp = * a; * a = * B; * B = temp;} void select_sort (int * a, int n) {assert (a); int I, j, min; for (I = 0; I <n-1; I ++) // I 0 ~ N-2 {min = a [I]; // each time you select a minimum value for (j = I; j <n; j ++) // j I ~ N-1 {if (a [j] <min) {swap (& (a [j]), & (a [I]) ;}}} int main () {int a [10] = {2, 1, 3, 4, 5, 7, 2, 3, 6, 1}; int num = sizeof (a)/sizeof (int ); printf ("before_sort:"); display (a, num); select_sort (a, num); printf ("after_sort:"); display (a, num ); return 0 ;}
# Include <stdio. h> # include <time. h> // exchange two data void swap (int * a, int * B) {int temp; temp = * a; * a = * B; * B = temp ;} // display the exchanged array void display_array (int a [], int n) {int I; for (I = 0; I <n; I ++) printf ("% d", a [I]);} // select the sort void select_sort (int a [], int n) {int I, j; for (I = 0; I <n-1; I ++) {int min = a [I]; // set the minimum number for each time, I 0 ~ 8for (j = I + 1; j <n; j ++) // convert a [I] ~ Compare the number of a [9] values with the current minimum number. if the number is smaller than the current minimum number, {if (a [j] <min) {swap (& (a [j]), & (a [I]) ;}}} int main () {clock_t start, finish; start = clock (); int n = 10; int a [] = {2, 1, 3, 4, 5, 7, 6, 8, 2, 4}; printf ("Before sorting :"); display_array (a, n); select_sort (a, n); printf ("After sorting:"); display_array (a, n); finish = clock (); printf ("\ n total computing time: % f seconds \ n", (double) (finish-start)/CLOCKS_PER_SEC); return 0 ;}