/* file introspectiveSort.h */ // code by bds, 3/00 #ifndef __INTROSPECTIVESORT__ #define __INTROSPECTIVESORT__ #include #include "iterator_traits" #include "heapSort.h" #include "insertionSort.h" #include "partition.h" template /* We require that class Iterator has pointer arithmetic operations and that the unknown class pointed to by dereferencing iterators has operator <= defined. */ void introspectiveSort(Iterator b, Iterator e, bool randomized = false) { introSort(b, e, static_cast( ceil(2*log(static_cast(e-b))) ), randomized ); } template void introSort(Iterator b, Iterator e, int d, bool randomized = false) { // The items [*b, *(b+1) .. *e) will be permuted into sorted order. // It's quickSort but // uses special handling of small sequences and deep recursion. if ((e-b) <= 15) // small sequence threshold. // then zero or one elements -- and that's already sorted. insertionSort(b, e); else if (d == 0) // reached recursion-is-too-deep-for-quickSort threshold. heapSort(b, e); else { Iterator p = partition(b, e, randomized); ++p; introSort(b, p, d-1); introSort(p, e, d-1); } } #endif