// File: qs-gen.cpp // // Generic Quicksort Routines // // #include this file with DATA defined appropriately. // int partition(DATA A[], int low, int high) { DATA x, temp ; int i, j ; i = low - 1; j = high + 1; x = A[low] ; while (true) { // Find an element less than x do { j = j - 1; } while (A[j] > x) ; // Find an element greater than x do { i = i + 1; } while (A[i] < x); // swap smaller and bigger elements, if needed // if (i < j) { temp = A[j] ; A[j] = A[i] ; A[i] = temp ; } else { return j; } } } void RecQuickSort(DATA 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(DATA A[], int n) { RecQuickSort(A, 0, n-1) ; }