/* File: tweak_quick.c Tweaking Quicksort for better performance */ #include #include #include "sorting.h" index partition(data A[], index low, index high) { data x, temp ; index i, j ; i = low - 1; j = high + 1; x = A[low] ; while (1) { /* 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); if (i < j) { temp = A[j] ; A[j] = A[i] ; A[i] = temp ; } else { return j; } } } void quicksort(data A[], index low, index high) { index q ; #ifndef NDEBUG printf("Quicksort: low = %d, high = %d\n", low, high) ; #endif if (high - low <= 15) { insertion_sort(A, low, high) ; return ; } q = partition(A, low, high) ; assert(q < high) ; quicksort(A, low, q) ; quicksort(A, q + 1, high) ; }