/* June 30, 1998 */ /* Program with a driver that calls a function select_sort to sort a set of array */ #include #define NUM 4 /* function prototype */ void select_sort(int list[], int n); int get_min_range(int list[], int first, int last); main () { /* declarations */ int a[NUM] = {74, 45, 83, 16}; int i; /* print original set of numbers */ printf("\nOriginal set of numbers:"); for (i = 0; i < NUM; ++i) { printf("%6d", a[i]); } printf("\n"); /* print sorted set of numbers */ printf(" Sorted set of numbers:"); select_sort(a, NUM); for (i = 0; i < NUM; ++i) { printf("%6d", a[i]); } printf("\n"); } /* function definition */ int get_min_range(int list[], int first, int last) { int j, small_sub; small_sub = first; for (j = first; j <= last; ++j) if (list[j] < list[small_sub]) small_sub = j; return (small_sub); } void select_sort(int list[], int n) { int fill, temp, index_of_min; for (fill = 0; fill < n-1; ++fill) { /* Find position of smallest element in unsorted subarray */ index_of_min = get_min_range(list, fill, n-1); /* Exchange elements at fill and index_of_min */ if (fill != index_of_min) { temp = list[index_of_min]; list[index_of_min] = list[fill]; list[fill] = temp; } } }