[prev] 9 [next]

Implementing Sorting

Concrete framework:

// we deal with generic Items
typedef int Item;

// abstractions to hide details of Items
#define key(A) (A)
#define less(A,B) (key(A) < key(B))
#define swap(A,B) {Item t; t = A; A = B; B = t;}
#define swil(A,B) {if (less(A,B)) swap(A,B);}
// swil = SWap If Less

// Sorts a slice of an array of Items
void sort(Item a[], int lo, int hi);

// Check for sortedness (to validate functions)
int isSorted(Item a[], int lo, int hi);