#include typedef int Item; #define key(A) (A) #define lt(A, B) (key(A) < key(B)) // less than #define le(A, B) (key(A) <= key(B)) // less than or equal to #define ge(A, B) (key(A) >= key(B)) // greater than or equal to #define gt(A, B) (key(A) > key(B)) // greater than void swap(Item a[], int i, int j) { Item temp = a[i]; a[i] = a[j]; a[j] = temp; } void selectionSort(Item items[], int lo, int hi) { int comparisons = 0; for (int i = lo; i < hi; i++) { int min = i; for (int j = i + 1; j <= hi; j++) { comparisons++; if (lt(items[j], items[min])) { min = j; } } swap(items, i, min); } printf("That took %d comparisons!\n", comparisons); } int main(void) { Item a[] = {42, 7, 19, 3, 88, 1}; int n = sizeof(a) / sizeof(a[0]); selectionSort(a, 0, n - 1); for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); return 0; }