|
ADS
Algorithms and Data Structures
|
Pigeonhole Sort (exercise 4.11) of the pairs \( ( \mathit{key} , \mathit{value} ) \) with keys in \( [ 0 , k ) \). More...
Files | |
| file | ADS/sort/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. | |
Functions | |
| template<class V> | |
| std::vector< std::pair< int, V > > | ADS::pigeonholeSort (const std::vector< std::pair< int, V > > &A, int k) |
| the pairs sorted by key, the values of a key in their first order | |
Pigeonhole Sort (exercise 4.11) of the pairs \( ( \mathit{key} , \mathit{value} ) \) with keys in \( [ 0 , k ) \).
The definitions, the invariants and the costs of the area are in Theory of sorting.
| std::vector< std::pair< int, V > > ADS::pigeonholeSort | ( | const std::vector< std::pair< int, V > > & | A, |
| int | k ) |
the pairs sorted by key, the values of a key in their first order
In pseudocode (exercise 4.11), one LIST per key:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{pigeonholeSort}( A , k ) \cr 2 & \quad B \gets k \text{ empty lists};\ \mathit{out} \gets \text{an empty vector} \cr 3 & \quad \textbf{foreach}\ ( \mathit{key} , v ) \in A\ \textbf{do} \ \text{append } v \text{ to } B[ \mathit{key} ]\quad \triangleright \ \text{stable} \cr 4 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ k - 1\ \textbf{do} \cr 5 & \qquad \textbf{foreach}\ v \in B[ i ] \text{ from its head} \ \textbf{do}\ \text{append } ( i , v ) \text{ to } \mathit{out} \cr 6 & \quad \textbf{return}\ \mathit{out} \end{array}} \]