// File: quicksort.cpp // // Quicksort Routines #ifndef QUICKSORT_CPP #define QUICKSORT_CPP #include #include using namespace std ; // Partition array into two parts. // First part has items <= pivot x. // Second part has items > pivot x. // The return value is the position of x. // template int partition(T A[], int low, int high) { T 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; } } } template void QuickSort(T A[], int low, int high) { int q ; // cout << "QuickSort: low = " << low << ", high = " << high << endl ; if (low >= high) return ; q = partition(A, low, high) ; assert(q < high) ; QuickSort(A, low, q) ; QuickSort(A, q + 1, high) ; } #endif