|
ADS
Algorithms and Data Structures
|
The internal sorts of Knuth that the book leaves out, beyond the book. More...
Files | |
| file | ADS/sort/KnuthSorts.h |
| The internal sorts of Knuth that the book leaves out, beyond the book: | |
Functions | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::binaryInsertionSort (Store &&A, C less=C()) |
| the binary insertion sort | |
| template<class T, class C = std::less<>> | |
| void | ADS::binaryInsertionSort (std::vector< T > &A, C less=C()) |
| binaryInsertionSort() above on a vector | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::listInsertionSort (Store &&A, C less=C(), std::size_t *moves=nullptr) |
| the list insertion sort; the moves of the items go to *moves, if given | |
| template<class T, class C = std::less<>> | |
| void | ADS::listInsertionSort (std::vector< T > &A, C less=C(), std::size_t *moves=nullptr) |
| listInsertionSort() above on a vector | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::addressCalculationSort (Store &&A, std::size_t M=0) |
| the address calculation sort of numbers into \( M \) lists, \( M = \lfloor n / 4 \rfloor + 1 \) if \( M \) is 0; with an infinite range (an infinity, or a span beyond a double) all the items go to one list, an insertion sort | |
| template<class T> | |
| void | ADS::addressCalculationSort (std::vector< T > &A, std::size_t M=0) |
| addressCalculationSort() above on a vector | |
| template<RankStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::radixExchangeSort (Store &&A) |
| the radix exchange sort | |
| template<RadixKey T> | |
| void | ADS::radixExchangeSort (std::vector< T > &A) |
| radixExchangeSort() above on a vector | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::tournamentSort (Store &&A, C less=C()) |
| the tournament sort | |
| template<class T, class C = std::less<>> | |
| void | ADS::tournamentSort (std::vector< T > &A, C less=C()) |
| tournamentSort() above on a vector | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::mergeInsertionSort (Store &&A, C less=C()) |
| the merge insertion sort of Ford and Johnson | |
| template<class T, class C = std::less<>> | |
| void | ADS::mergeInsertionSort (std::vector< T > &A, C less=C()) |
| mergeInsertionSort() above on a vector | |
| long | ADS::fordJohnsonBound (std::size_t n) |
| the worst case of the comparisons of mergeInsertionSort() on \( n \) items, \( \sum_{k=1}^{n} \lceil \log_2 ( 3 k / 4 ) \rceil \) | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { binaryInsertionSort( v , std::forward< P > ( p )... ); } | |
| decltype(auto) | ADS::binaryInsertionSort (R &&A, P &&... p) |
| binaryInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { listInsertionSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::listInsertionSort (R &&A, P &&... p) |
| listInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { addressCalculationSort( v , std::forward< P > ( p )... ); } | |
| decltype(auto) | ADS::addressCalculationSort (R &&A, P &&... p) |
| addressCalculationSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { radixExchangeSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::radixExchangeSort (R &&A, P &&... p) |
| radixExchangeSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { tournamentSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::tournamentSort (R &&A, P &&... p) |
| tournamentSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { mergeInsertionSort( v , std::forward< P > ( p )... ); } | |
| decltype(auto) | ADS::mergeInsertionSort (R &&A, P &&... p) |
| mergeInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only | |
The internal sorts of Knuth that the book leaves out, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of sorting.
| decltype(auto) ADS::addressCalculationSort | ( | R && | A, |
| P &&... | p ) |
addressCalculationSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::addressCalculationSort | ( | std::vector< T > & | A, |
| std::size_t | M = 0 ) |
addressCalculationSort() above on a vector
| void ADS::addressCalculationSort | ( | Store && | A, |
| std::size_t | M = 0 ) |
the address calculation sort of numbers into \( M \) lists, \( M = \lfloor n / 4 \rfloor + 1 \) if \( M \) is 0; with an infinite range (an infinity, or a span beyond a double) all the items go to one list, an insertion sort
In pseudocode (Isaac and Singleton, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{addressCalculationSort}( A , n , M ) \cr 2 & \quad \mathit{lo} \gets \min A;\ w \gets \max A - \mathit{lo} \cr 3 & \quad \textbf{foreach}\ x \in A\ \textbf{do} \cr 4 & \qquad i \gets \min\{ M - 1 , \lfloor ( x - \mathit{lo} ) M / w \rfloor \} \cr 5 & \qquad \text{insert } x \text{ in the sorted list } B[ i ] \text{, } \text{after its equals} \cr 6 & \quad A \gets B[ 0 ] \cdot B[ 1 ] \cdots B[ M - 1 ] \end{array}} \]
where \( i = 0 \) when \( w \) is 0 or infinite.
| decltype(auto) ADS::binaryInsertionSort | ( | R && | A, |
| P &&... | p ) |
binaryInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::binaryInsertionSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
binaryInsertionSort() above on a vector
| void ADS::binaryInsertionSort | ( | Store && | A, |
| C | less = C() ) |
the binary insertion sort
In pseudocode (Knuth, section 5.2.1, beyond the book), each item placed after its equals:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{binaryInsertionSort}( A , n ) \cr 2 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad \mathit{lo} \gets 0;\ \mathit{hi} \gets i \cr 4 & \qquad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 5 & \qquad\quad m \gets \lfloor ( \mathit{lo} + \mathit{hi} ) / 2 \rfloor \cr 6 & \qquad\quad \textbf{if}\ A[ i ] < A[ m ]\ \textbf{then}\ \mathit{hi} \gets m\ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 7 & \qquad \text{rotate } A[ \mathit{lo} .. i ] \text{ one place to the } \text{right} \end{array}} \]
|
inline |
the worst case of the comparisons of mergeInsertionSort() on \( n \) items, \( \sum_{k=1}^{n} \lceil \log_2 ( 3 k / 4 ) \rceil \)
| decltype(auto) ADS::listInsertionSort | ( | R && | A, |
| P &&... | p ) |
listInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::listInsertionSort | ( | std::vector< T > & | A, |
| C | less = C(), | ||
| std::size_t * | moves = nullptr ) |
listInsertionSort() above on a vector
| void ADS::listInsertionSort | ( | Store && | A, |
| C | less = C(), | ||
| std::size_t * | moves = nullptr ) |
the list insertion sort; the moves of the items go to *moves, if given
In pseudocode (Algorithm 5.2.1L of Knuth, beyond the book), \( L \) the links of a list whose head is \( L[ 0 ] \), the item \( A[ j ] \) its node \( j + 1 \) and 0 its end:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{listInsertionSort}( A , n ) \cr 2 & \quad L[ 0 ] \gets n;\ L[ n ] \gets 0 \cr 3 & \quad \textbf{for}\ j \gets n - 2\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad p \gets 0;\ q \gets L[ 0 ] \cr 5 & \qquad \textbf{while}\ q \ne 0\ \textbf{and}\ A[ q - 1 ] < A[ j ] \ \textbf{do}\ p \gets q;\ q \gets L[ q ] \cr 6 & \qquad L[ p ] \gets j + 1;\ L[ j + 1 ] \gets q \cr 7 & \quad \text{copy the items in the order of the list, from } L[ 0 ] \end{array}} \]
| decltype(auto) ADS::mergeInsertionSort | ( | R && | A, |
| P &&... | p ) |
mergeInsertionSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::mergeInsertionSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
mergeInsertionSort() above on a vector
| void ADS::mergeInsertionSort | ( | Store && | A, |
| C | less = C() ) |
the merge insertion sort of Ford and Johnson
In pseudocode (Ford and Johnson, section 5.3.1 of Knuth, beyond the book), \( t_k = ( 2^{k+1} + ( -1 )^k ) / 3 \) the Jacobsthal numbers \( 1 , 3 , 5 , 11 , 21 , \ldots \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fordJohnson}( v ) \cr 2 & \quad \textbf{if}\ |v| \le 1\ \textbf{then return}\ v \cr 3 & \quad \text{pair } v[ 0 ] , v[ 1 ] \text{, then } v[ 2 ] , v[ 3 ] \text{, and so on, by one comparison each} \cr 4 & \quad ( l_1 , \ldots , l_k ) \gets \mathrm{fordJohnson}( \text{the } \text{larger item of each pair} ) \cr 5 & \quad s_i \gets \text{the partner of } l_i;\ s_{k+1} \gets \text{the } \text{item left alone, if } |v| \text{ is odd} \cr 6 & \quad C \gets ( s_1 , l_1 , \ldots , l_k ) \cr 7 & \quad \textbf{for}\ j = 2 , 3 , \ldots\ \textbf{do} \cr 8 & \qquad \textbf{for}\ i \gets \min\{ t_j , \text{the last } s \} \ \textbf{downto}\ t_{j-1} + 1\ \textbf{do} \cr 9 & \qquad\quad \text{insert } s_i \text{ in } C \text{ by binary search } \text{before } l_i \cr 10 & \quad \textbf{return}\ C \cr 11 & \textbf{procedure}\ \mathrm{mergeInsertionSort}( A ) \cr 12 & \quad A \gets \text{the items of } A \text{ in the order of } \mathrm{fordJohnson}( 0 , \ldots , n - 1 ) \end{array}} \]
| decltype(auto) ADS::radixExchangeSort | ( | R && | A, |
| P &&... | p ) |
radixExchangeSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::radixExchangeSort | ( | std::vector< T > & | A | ) |
radixExchangeSort() above on a vector
| void ADS::radixExchangeSort | ( | Store && | A | ) |
the radix exchange sort
In pseudocode (Algorithm 5.2.2R of Knuth, beyond the book), \( b \) the bit examined, from the most significant one \( w - 1 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{radixExchange}( A , \mathit{lo} , \mathit{hi} , b ) \cr 2 & \quad \textbf{if}\ \mathit{hi} - \mathit{lo} \le 1\ \textbf{or}\ b < 0 \ \textbf{then return} \cr 3 & \quad i \gets \mathit{lo};\ j \gets \mathit{hi} \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{while}\ i < j \text{ and the bit } b \text{ of } A[ i ] \text{ is 0}\ \textbf{do}\ i \gets i + 1 \cr 6 & \qquad \textbf{while}\ i < j \text{ and the bit } b \text{ of } A[ j - 1 ] \text{ is 1}\ \textbf{do}\ j \gets j - 1 \cr 7 & \qquad \textbf{if}\ i \ge j\ \textbf{then}\ \textbf{break} \cr 8 & \qquad A[ i ] \leftrightarrow A[ j - 1 ];\ i \gets i + 1;\ j \gets j - 1 \cr 9 & \quad \mathrm{radixExchange}( A , \mathit{lo} , i , b - 1 ) \cr 10 & \quad \mathrm{radixExchange}( A , i , \mathit{hi} , b - 1 ) \cr 11 & \textbf{procedure}\ \mathrm{radixExchangeSort}( A , n ) \cr 12 & \quad \mathrm{radixExchange}( A , 0 , n , w - 1 ) \end{array}} \]
| decltype(auto) ADS::tournamentSort | ( | R && | A, |
| P &&... | p ) |
tournamentSort() 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, on its view otherwise, since the sort reads the items by their ranks only
| void ADS::tournamentSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
tournamentSort() above on a vector
| void ADS::tournamentSort | ( | Store && | A, |
| C | less = C() ) |
the tournament sort
In pseudocode (the tree selection of Knuth, section 5.2.3, beyond the book), \( W[ v ] \) the winner of the match at the node \( v \) of a complete tree with \( L \ge n \) leaves, \( \bot \) for no item:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{tournamentSort}( A , n ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ L - 1\ \textbf{do}\ W[ L + i ] \gets i \text{ if } i < n \text{, else } \bot \cr 3 & \quad \textbf{for}\ v \gets L - 1\ \textbf{downto}\ 1\ \textbf{do}\ W[ v ] \gets \mathrm{play}( W[ 2 v ] , W[ 2 v + 1 ] ) \cr 4 & \quad \textbf{for}\ k \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad B[ k ] \gets A[ W[ 1 ] ];\ v \gets L + W[ 1 ];\ W[ v ] \gets \bot \cr 6 & \qquad \textbf{while}\ v > 1\ \textbf{do}\ v \gets \lfloor v / 2 \rfloor;\ W[ v ] \gets \mathrm{play}( W[ 2 v ] , W[ 2 v + 1 ] ) \cr 7 & \quad A \gets B \end{array}} \]
where \( \mathrm{play}( a , b ) \) returns the smaller item, \( a \) on a tie, and the one that is not \( \bot \) against \( \bot \).