|
ADS
Algorithms and Data Structures
|
Files | |
| CountingSort.h | |
| Counting Sort (section 2.7.5) of integers in \( [ 0 , k ) \): \( B[ v ] \) counts the occurrences of \( v \), then each \( v \) is written \( B[ v ] \) times in order; \( \Theta( n + k ) \) time and \( \Theta( k ) \) space, linear when \( k = O( n ) \). | |
| EngineeredQuickSorts.h | |
| The quicksorts beyond the quicksort of Montresor's book [see QuickSort.h for it and its improvements of section 12.2]: the two of chapter 5, Sorting Atomic Items, of Ferragina's book, and the one engineered for speed, pdqSort(), which is in PdqSort.h [see PdqSort.h, which this header includes]: | |
| ExchangeSorts.h | |
| The sorts by exchanges of items, beyond the book, all in place: | |
| ExternalSort.h | |
| The sort of a file larger than the memory, beyond the book, as Knuth describes it for tapes, here on sequences in memory that play the files: | |
| Heapsort.h | |
| The max-heap on a vector and heapsort (section 10.1.4): the children of the place \( i \) are \( 2 i + 1 \) and \( 2 i + 2 \), and a max-heap has \( A[ \lfloor ( i - 1 ) / 2 \rfloor ] \ge A[ i ] \) for each \( i > 0 \). | |
| InsertionSort.h | |
| Insertion Sort (section 2.7.2), as one sorts a hand of cards: \( A[ i ] \) is inserted in the sorted prefix \( A[ 0 .. i - 1 ] \), the larger items moving one place to the right. | |
| KnuthSorts.h | |
| The internal sorts of Knuth that the book leaves out, beyond the book: | |
| LearnedSort.h | |
| The learned sort, beyond the book: learnedSort() distributes the numbers of 32 or 64 bits into buckets by a model of the cumulative distribution function (CDF) of their keys, learned from a sample, and sorts each bucket by vectorQuickSort() [see VectorQuickSort.h]; it falls back to sampleSort() [see SampleSort.h] when the sample shows a distribution that the model does not fit. | |
| MergeSort.h | |
| Merge Sort (section 2.7.3): the two halves of the vector are sorted recursively and merged by mergeParts(), the Merge() of the book, through a vector \( B \) allocated once, \( T( n ) = 2 T( n / 2 ) + \Theta( n ) = \Theta( n \log n ) \) time, optimal among the sorts by comparisons, \( \Theta( n ) \) extra space, stable (an item of the left part goes first when equal). | |
| MultikeyQuickSort.h | |
| The multikey quicksort of Bentley and Sedgewick, as chapter 7, Sorting Strings, of Ferragina's book presents it: the quicksort of strings by one character at a time, a partition into three parts, the strings whose character \( d \) is smaller, equal and larger than that of the pivot, the equal part going on with the character \( d + 1 \) and the others with \( d \). | |
| PdqSort.h | |
| The pattern-defeating quicksort, beyond the book and beyond the quicksorts of Ferragina's book, which are in EngineeredQuickSorts.h [see EngineeredQuickSorts.h, which includes this header]: | |
| PigeonholeSort.h | |
| Pigeonhole Sort (exercise 4.11) of the pairs \( ( \mathit{key} , \mathit{value} ) \) with keys in \( [ 0 , k ) \): the value of each pair goes at the end of the LIST of its key, then the lists are read in the order of the keys; \( O( n + k ) \) time, and stable, unlike Counting Sort, which only counts the keys. | |
| QuickSort.h | |
| Quicksort (section 12.2): partition(), the perno() of the book, rearranges \( A[ \mathit{start} .. \mathit{end} ] \) around the pivot \( x = A[ \mathit{start} ] \), so that the items before its final place \( j \) are smaller and the ones after it are not, and the two parts are sorted recursively. | |
| QuicksortAdversary.h | |
| The adversary of McIlroy against quicksort, beyond the book: a comparator of the items \( 0 , \ldots , n - 1 \) that does not look at their values, since they have none when the sort starts, and gives them their values while the sort runs, so that the quicksort that it is passed to makes \( \Theta( n^2 ) \) comparisons (exercise 11.6 of Morin's book). | |
| RadixSort.h | |
| The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort, the one-digit cases, in CountingSort.h and PigeonholeSort.h): | |
| RunSorts.h | |
| The natural merge sort that adapts to the runs of the input, beyond the book [see naturalMergeSort() of SequenceAlgorithms.h for the plain one], stable: | |
| SampleSort.h | |
| The in-place super scalar samplesort, beyond the book: sampleSort() splits the vector into up to 256 buckets by splitters drawn from a sample, in place with \( O( k b ) \) extra items for \( k \) buckets and blocks of \( b \) items (about 2 KiB), and sorts each bucket in the same way, those of at most 64 items by insertion. | |
| SelectionSort.h | |
| Selection Sort (section 2.7.1): the minimum of the part still to sort \( A[ i .. n - 1 ] \) is swapped with \( A[ i ] \), extending the sorted prefix; \( \sum_{i=0}^{n-2} ( n - i - 1 ) = n ( n - 1 ) / 2 \) comparisons whatever the order of the input, \( \Theta( n^2 ) \) time, \( O( 1 ) \) space, not stable. | |
| ShellSort.h | |
| Shell Sort (section 15.1), a local search that lowers the number of inversions: an insertion sort of the items \( h \) apart, for decreasing gaps \( h \) down to 1, so that the far items move first and the last pass, the insertion sort, finds the vector almost sorted. | |
| Sort.h | |
| The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort and pick, among the sorts of ADS, the one that suits it. | |
| Sorted.h | |
| The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the audits are on (ADS_CONTRACTS at least 3), and the check that the output is sorted and holds the same items as the input, which sorts a copy of the input with std::sort and compares the two item by item through the equivalence of the order, in \( O( n \log n ) \) time instead of the \( O( n^2 ) \) of std::is_permutation with a predicate; and the check that a sequence is sorted. | |
| SortingNetworks.h | |
| Sorting networks, beyond the book: sequences of comparators \( ( i , j ) \), \( i \ne j \), each putting the smaller of \( A[ i ] \) and \( A[ j ] \) in \( A[ i ] \), fixed in advance whatever the input, so that the comparators of a round touch disjoint places and run in parallel (in hardware, on SIMD lanes, on a GPU): | |
| StrandSort.h | |
| The strand sort, beyond the book, on a list with pointers [see List.h]: a strand, i.e., the first item and every later one not smaller than the last taken, is pulled out of the list and merged with the result, until the list is empty. | |
| StringSort.h | |
| The sort of strings by integer keys of their prefixes, beyond the book: prefixKeySort() reads each string a few times in all, instead of once per pass or per comparison, and leaves the work to the quicksort of integers in the vector registers [see VectorQuickSort.h]. | |
| TreeSort.h | |
| The tree sort (exercise 6.1 and its solution): the items are inserted in a search tree [see SearchTree.h] and read back by the symmetric visit, which gives the keys in increasing order; each key keeps the number of its copies, so that the repeated items are not lost. | |
| VectorQuickSort.h | |
| The quicksort in the lanes of the vector registers, beyond the book: vectorQuickSort() sorts integers and floating point numbers of 32 or 64 bits by their natural order. | |