/* insertionSort.cc for use with sortMain.cc */ // code by david saunders (taken from Sedgewick, page 100). 2/96 typedef int itemType; // Must be a type on which the comparator > can be used. void insertionSort(itemType a[], int N){ // The items in positions a[1..N] are permuted into sorted order. // Side effect: a[0] is overwritten. int i, j; itemType v; for (i = 2; i <= N; i++) { // invariant: a[1..i-1] is sorted. v = a[i]; j = i; a[0] = v; // sentinel for while loop. while (a[j-1] > v) { a[j] = a[j-1]; j--; } a[j] = v; } // Now a[1..i-1] is sorted and i = N+1. // Therefore a[1..N] is sorted. } char* id = "ins"; char* ate = "g(N) := N^2;"; void sort(itemType a[], int M, int N){insertionSort(a, N);}