|
ADS
Algorithms and Data Structures
|
The pattern-defeating quicksort, beyond the book and beyond the quicksorts of Ferragina's book, which are in EngineeredQuickSorts.h. More...
Files | |
| file | ADS/sort/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]: | |
Functions | |
| template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::pdqSort (Store &&A, C less=C()) |
| the pattern-defeating quicksort | |
| template<class T, class C = std::less<>> | |
| void | ADS::pdqSort (std::vector< T > &A, C less=C()) |
| pdqSort() above on a vector | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { pdqSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::pdqSort (R &&A, P &&... p) |
| pdqSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses | |
The pattern-defeating quicksort, beyond the book and beyond the quicksorts of Ferragina's book, which are in EngineeredQuickSorts.h.
The definitions, the invariants and the costs of the area are in Theory of sorting.
| decltype(auto) ADS::pdqSort | ( | R && | A, |
| P &&... | p ) |
| void ADS::pdqSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
pdqSort() above on a vector
| void ADS::pdqSort | ( | Store && | A, |
| C | less = C() ) |
the pattern-defeating quicksort
In pseudocode (Peters, beyond the book), \( \mathit{bad} \) starting from \( \lfloor \log_2 n \rfloor + 1 \) and the part leftmost when no item precedes it:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{pdq}( A , \mathit{lo} , \mathit{hi} , \mathit{bad} ) \cr 2 & \quad \textbf{loop} \cr 3 & \qquad \textbf{if}\ \mathit{hi} - \mathit{lo} < 24\ \textbf{then} \ \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} ); \ \textbf{return} \cr 4 & \qquad \text{the median of three, or the ninther, to } A[ \mathit{lo} ] \cr 5 & \qquad \textbf{if}\ \text{not leftmost and } A[ \mathit{lo} - 1 ] \ge A[ \mathit{lo} ]\ \textbf{then} \cr 6 & \qquad\quad \mathit{lo} \gets 1 + \text{the end of the equals of the } \text{pivot, partitioned left};\ \textbf{continue} \cr 7 & \qquad p \gets \text{the place of the pivot, the equals on the right} \cr 8 & \qquad \textbf{if}\ \text{a side is smaller than } ( \mathit{hi} - \mathit{lo} ) / 8\ \textbf{then} \cr 9 & \qquad\quad \mathit{bad} \gets \mathit{bad} - 1 \cr 10 & \qquad\quad \textbf{if}\ \mathit{bad} = 0\ \textbf{then} \ \text{heapsort } A[ \mathit{lo} .. \mathit{hi} - 1 ];\ \textbf{return} \cr 11 & \qquad\quad \text{swap a few items of each side} \cr 12 & \qquad \textbf{else if}\ \text{nothing moved and both sides sort by } \text{insertion in 8 moves}\ \textbf{then return} \cr 13 & \qquad \mathrm{pdq}( A , \mathit{lo} , p , \mathit{bad} ); \ \mathit{lo} \gets p + 1 \end{array}} \]