|
ADS
Algorithms and Data Structures
|
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). More...
Files | |
| file | ADS/sort/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): | |
Concepts | |
| concept | ADS::RadixKey |
| an integer or a floating point number of at most 8 bytes | |
Functions | |
| template<RadixKey T> | |
| auto | ADS::radixKey (T x) |
| the unsigned key of \( x \), whose order is the order of the numbers: the bits of \( x \) with the sign bit flipped for a signed integer, and for a floating point number all of them flipped if it is negative and the sign bit otherwise | |
| template<SortStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::radixSort (Store &&A) |
| the LSD radix sort of numbers | |
| template<RadixKey T> | |
| void | ADS::radixSort (std::vector< T > &A) |
| radixSort() above on a vector | |
| template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class K> | |
| void | ADS::radixSortBy (Store &&A, K key) |
| the LSD radix sort of any items by the unsigned key( x ), stable | |
| template<class T, class K> | |
| void | ADS::radixSortBy (std::vector< T > &A, K key) |
| radixSortBy() above on a vector | |
| template<SortStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >> | |
| void | ADS::americanFlagSort (Store &&A) |
| the MSD radix sort of numbers in place | |
| template<RadixKey T> | |
| void | ADS::americanFlagSort (std::vector< T > &A) |
| americanFlagSort() above on a vector | |
| template<StringStore Store> | |
| void | ADS::msdRadixSort (Store &&A) |
| the MSD radix sort of strings | |
| void | ADS::msdRadixSort (std::vector< std::string > &A) |
| msdRadixSort() above on a vector | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { radixSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::radixSort (R &&A, P &&... p) |
| radixSort() 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 | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { radixSortBy( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::radixSortBy (R &&A, P &&... p) |
| radixSortBy() 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 | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { americanFlagSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::americanFlagSort (R &&A, P &&... p) |
| americanFlagSort() 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 | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { msdRadixSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::msdRadixSort (R &&A, P &&... p) |
| msdRadixSort() 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 | |
Variables | |
| constexpr std::size_t | ADS::kFlagSmall = kVectorSort ? 8192 : 256 |
| the groups that americanFlagSort() leaves to a sort by comparisons are those of fewer items than this: many with the vector registers, whose quicksort beats the radix passes that it replaces, few otherwise | |
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).
The definitions, the invariants and the costs of the area are in Theory of sorting.
| decltype(auto) ADS::americanFlagSort | ( | R && | A, |
| P &&... | p ) |
americanFlagSort() 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
| void ADS::americanFlagSort | ( | std::vector< T > & | A | ) |
americanFlagSort() above on a vector
| void ADS::americanFlagSort | ( | Store && | A | ) |
the MSD radix sort of numbers in place
In pseudocode (McIlroy, Bostic and McIlroy, and Skarupke, beyond the book), \( b_d( x ) \) the byte \( d \) of the key of \( x \), \( D \) the bytes in which some keys differ, the most significant first, and \( \mathrm{small} \) the sort of vectorQuickSort() or pdqSort():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{flag}( A , \mathit{lo} , \mathit{hi} , D ) \cr 2 & \quad d \gets \text{the first of } D;\ c[ b ] \gets \text{the items } \text{of } A[ \mathit{lo} .. \mathit{hi} - 1 ] \text{ with } b_d = b \cr 3 & \quad \textbf{if}\ \text{one } c[ b ] \text{ is all of them} \ \textbf{then}\ \mathrm{flag}( A , \mathit{lo} , \mathit{hi} , D \setminus d );\ \textbf{return} \cr 4 & \quad \mathit{next}[ b ] \gets \mathit{lo} + \textstyle\sum_{b' < b} c[ b' ];\ \mathit{end}[ b ] \gets \mathit{next}[ b ] + c[ b ] \cr 5 & \quad \textbf{while}\ \text{two buckets have } \mathit{next}[ b ] < \mathit{end}[ b ]\ \textbf{do} \cr 6 & \qquad \textbf{foreach}\ b \text{ with } \mathit{next}[ b ] < \mathit{end}[ b ]\ \textbf{do} \cr 7 & \qquad\quad \textbf{for}\ i \gets \mathit{next}[ b ]\ \textbf{to} \ \mathit{end}[ b ] - 1\ \textbf{do} \cr 8 & \qquad\qquad e \gets b_d( A[ i ] );\ A[ i ] \leftrightarrow A[ \mathit{next}[ e ] ];\ \mathit{next}[ e ] \gets \mathit{next}[ e ] + 1 \cr 9 & \quad \textbf{if}\ D \ne \{ d \}\ \textbf{then foreach}\ b \text{ with } c[ b ] > 1\ \textbf{do} \cr 10 & \qquad \textbf{if}\ c[ b ] \ge \mathtt{kFlagSmall}\ \textbf{then} \ \mathrm{flag}( \text{the bucket } b , D \setminus d ) \ \textbf{else}\ \mathrm{small}( \text{the bucket } b ) \cr 11 & \textbf{procedure}\ \mathrm{americanFlagSort}( A , n ) \cr 12 & \quad \textbf{if}\ A \text{ is sorted}\ \textbf{then return} \cr 13 & \quad \textbf{if}\ A \text{ is non-increasing}\ \textbf{then} \ \text{reverse } A;\ \textbf{return} \cr 14 & \quad \textbf{if}\ | D | = 1\ \textbf{then}\ \text{count the keys} \text{ and write them back};\ \textbf{return} \cr 15 & \quad \textbf{if}\ n < \mathtt{kFlagSmall}\ \textbf{then} \ \mathrm{small}( A )\ \textbf{else}\ \mathrm{flag}( A , 0 , n , D ) \end{array}} \]
where the loop of the step 7 runs over the part of the bucket that was not in place when the round started, and the order of the keys is the one of radixKey(): \( -0 \) before \( +0 \), the NaN at the ends.
| decltype(auto) ADS::msdRadixSort | ( | R && | A, |
| P &&... | p ) |
msdRadixSort() 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
|
inline |
msdRadixSort() above on a vector
| void ADS::msdRadixSort | ( | Store && | A | ) |
the MSD radix sort of strings
In pseudocode (McIlroy, Bostic and McIlroy, beyond the book), \( c_d( s ) \) the character \( d \) of \( s \) plus 1, 0 past its end, and the strings of a group sharing their first \( d \) characters:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{msd}( S , d ) \cr 2 & \quad \textbf{if}\ |S| \le 16\ \textbf{then}\ \text{sort } S \text{ } \text{by insertion on the suffixes from } d;\ \textbf{return} \cr 3 & \quad d \gets d + \text{the length of the prefix that the strings of } S \text{ share from } d \cr 4 & \quad \textbf{if}\ \text{the strings of } S \text{ are all equal} \ \textbf{then return} \cr 5 & \quad \text{distribute } S \text{ into the buckets } S_0 , \ldots , S_{256} \text{ by } c_d \cr 6 & \quad \textbf{for}\ r \gets 1\ \textbf{to}\ 256\ \textbf{do} \ \textbf{if}\ |S_r| \ge 2\ \textbf{then}\ \mathrm{msd}( S_r , d + 1 ) \cr 7 & \textbf{procedure}\ \mathrm{msdRadixSort}( A ) \cr 8 & \quad \textbf{if}\ n > 1\ \textbf{then}\ \mathrm{msd}( A , 0 ) \end{array}} \]
where the bucket \( S_0 \) holds the strings that end at \( d \), all equal, the first distribution permutes the strings in place as americanFlagSort() does, and the later ones move them between the vector and a buffer.
|
nodiscard |
the unsigned key of \( x \), whose order is the order of the numbers: the bits of \( x \) with the sign bit flipped for a signed integer, and for a floating point number all of them flipped if it is negative and the sign bit otherwise
| decltype(auto) ADS::radixSort | ( | R && | A, |
| P &&... | p ) |
radixSort() 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
| void ADS::radixSort | ( | std::vector< T > & | A | ) |
radixSort() above on a vector
| void ADS::radixSort | ( | Store && | A | ) |
the LSD radix sort of numbers
In pseudocode (the LSD radix sort of Cormen, Leiserson, Rivest and Stein, section 8.3, on bytes, beyond the book), \( b_j( x ) \) the byte \( j \) of the key of \( x \) from the least significant and \( B \) a buffer of \( n \) places:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{radixSort}( A , n ) \cr 2 & \quad D \gets \text{the bytes } j \text{ in which some two keys differ} \cr 3 & \quad \textbf{if}\ D = \emptyset\ \textbf{then return} \cr 4 & \quad \textbf{if}\ |D| = 1\ \textbf{then}\ \text{count the keys and } \text{write them back in order};\ \textbf{return} \cr 5 & \quad \text{count the bytes } b_j( x ) \text{ of all the items, for } \text{all } j \in D \text{, in one sweep} \cr 6 & \quad \textbf{foreach}\ j \in D \text{ in increasing order} \ \textbf{do} \cr 7 & \qquad \text{scatter } A \text{ into } B \text{ by } b_j \text{, } \text{stably, from the prefix sums of the counts} \cr 8 & \qquad A \leftrightarrow B \cr 9 & \quad \textbf{if}\ \text{the items ended in the buffer}\ \textbf{then} \ \text{copy them to } A \end{array}} \]
where a vector larger than the cache, whose keys differ in at least 3 bytes, is first scattered by its top byte of \( D \), and each part is sorted in the same way.
| decltype(auto) ADS::radixSortBy | ( | R && | A, |
| P &&... | p ) |
radixSortBy() 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
| void ADS::radixSortBy | ( | std::vector< T > & | A, |
| K | key ) |
radixSortBy() above on a vector
| void ADS::radixSortBy | ( | Store && | A, |
| K | key ) |
the LSD radix sort of any items by the unsigned key( x ), stable
It is the pseudocode of radixSort() with \( b_j( x ) \) the byte \( j \) of key( x ), and without the step 4, since equal keys need not be equal items.
|
inlineconstexpr |
the groups that americanFlagSort() leaves to a sort by comparisons are those of fewer items than this: many with the vector registers, whose quicksort beats the radix passes that it replaces, few otherwise