(* Quicksort with a different sorting algorithm applied to small files *) (* Baase, 1988, p.60 *) procedure Quicksort(first, last: Index); var splitPoint: Index; begin if last - first .GEQ. smallSize then Split(first, last, splitPoint); (* Partition *) Quicksort(first, splitPoint - 1); Quickstort(splitPoint + 1, last); else OtherSort(first, last) (* E.g., insertion sort *) end (* if *) end (* Quicksort *) /* Non-recursive quicksort, with the larger of the two subfiles put on the stack first. This is Program 7.3 in: Sedgewick, Robert. _Algorithms in C_, 3rd ed. Addison-Wesley, 1998. I added some comments. */ #define push2(A, B) push(B); push(A); void quicksort(Item a[], int l, int r) { int i; stackinit(); push2(l, r); while(!stackempty()) { l = pop(); r = pop(); if (r <= l) continue; // skip the remainder of the body of while i = partition(a, l, r); if (i-l > r-i) // choose the larger subfile { push2(l, i-1); push2(i+1, r); } else { push2(i+1, r); push2(l, i-1); } } }