// File: quicksort4.C // // template "implementation" for Quicksort #include "ordered.h" #include "quicksort4.h" //========================================================================== // local function prototypes // static int partition(Ordered **A, int low, int high) ; static void RecQuickSort(Ordered **A, int low, int high) ; //========================================================================== // function definitions // static int partition(Ordered **A, int low, int high) { Ordered *x, *temp ; int i, j ; i = low - 1; j = high + 1; x = A[low]->clone() ; while (true) { // Find an element less than x do { j = j - 1; } while (A[j]->cmp(x) > 0) ; // Find an element greater than x do { i = i + 1; } while (A[i]->cmp(x) < 0); // swap smaller and bigger elements, if needed // if (i < j) { temp = A[j] ; A[j] = A[i] ; A[i] = temp ; } else { return j; } } } static void RecQuickSort(Ordered **A, int low, int high) { int q ; if (low >= high) return ; q = partition(A, low, high) ; RecQuickSort(A, low, q) ; RecQuickSort(A, q + 1, high) ; } void QuickSort(Ordered **A, int n) { RecQuickSort(A, 0, n-1) ; }