|
ADS
Algorithms and Data Structures
|
The engineered selection, beyond the book. More...
Files | |
| file | ADS/search/NthElement.h |
| The engineered selection, beyond the book [see Selection.h for the selection of Hoare of section 17.1 and the median of the medians]: nthElement() puts in the place nth of the range \( [ \mathit{first} , \mathit{last} ) \) the item that would be there if the range were sorted, the items before it not larger and those after it not smaller, as std::nth_element, which it is meant to replace. | |
Functions | |
| template<std::random_access_iterator It, class C = std::less<>> | |
| void | ADS::nthElement (It first, It nth, It last, C less=C()) |
| the item of its rank put in nth, in \( [ \mathit{first} , \mathit{last} ) \) by less(), the items before it not larger and those after it not smaller | |
| template<class T, class C = std::less<>> | |
| T | ADS::nthElement (std::vector< T > &A, std::size_t k, C less=C()) |
| the item of rank \( k \) (from 0) of \( A \), which is rearranged by nthElement() above | |
The engineered selection, beyond the book.
The definitions, the invariants and the costs of the area are in Theory of searching.
| void ADS::nthElement | ( | It | first, |
| It | nth, | ||
| It | last, | ||
| C | less = C() ) |
the item of its rank put in nth, in \( [ \mathit{first} , \mathit{last} ) \) by less(), the items before it not larger and those after it not smaller
In pseudocode (the selection of Hoare with the sample of Floyd and Rivest, beyond the book), \( A[ 0 .. n - 1 ] \) the range and \( k \) the place nth:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{nthElement}( A , k ) \cr 2 & \quad \textbf{if}\ n > 600 \text{ and the 64 items at a stride rise} \text{ (fall)}\ \textbf{then} \cr 3 & \qquad \textbf{if}\ A \text{ is sorted (reversed, and then } \text{reversed back)}\ \textbf{then return} \cr 4 & \quad \mathit{budget} \gets 4 n \cr 5 & \quad \textbf{while}\ \text{the part } A[ i .. j ] \text{ has more } \text{than 24 items}\ \textbf{do} \cr 6 & \qquad \textbf{if}\ \mathit{budget} < j - i + 1\ \textbf{then}\ p \gets \text{the median of the medians of the groups of 5} \cr 7 & \qquad \textbf{else} \cr 8 & \qquad\quad \mathit{budget} \gets \mathit{budget} - ( j - i + 1 ) \cr 9 & \qquad\quad \textbf{if}\ j - i + 1 > 600\ \textbf{then} \ \mathrm{nthElement}( A[ \ell .. r ] , k );\ p \gets k\quad \triangleright\ \text{the sample of Floyd and Rivest} \cr 10 & \qquad\quad \textbf{else}\ p \gets \text{the median of } A[ i ] , A[ ( i + j ) / 2 ] , A[ j ] \cr 11 & \qquad m \gets \text{the place of } A[ p ] \text{ after a partition} \text{ of } A[ i .. j ] \text{ around it} \cr 12 & \qquad \textbf{if}\ m = k\ \textbf{then return} \cr 13 & \qquad \textbf{else if}\ k < m\ \textbf{then}\ j \gets m - 1 \ \textbf{else}\ i \gets m + 1 \cr 14 & \quad \text{sort } A[ i .. j ] \text{ by insertion} \end{array}} \]
where the partition is that of Lomuto with no branch on a part with many turns, that of Hoare on a part in some order, and puts the items equal to the pivot on its left when the pivot is the smallest item of the part; after the median of the medians, if \( k > m \) and the items after \( m \) are more than \( 7 ( j - i + 1 ) / 10 + 2 \), a second partition of \( A[ m .. j ] \) puts the items equal to the pivot on its left too, and \( m \) becomes the place of the last of them. The partition of Lomuto with no branch, the pivot \( p \) in \( A[ 0 ] \) and \( [ \cdot ] \) 1 when its condition holds and 0 otherwise, is
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{selectLomuto}( A[ 0 .. n - 1 ] ) \cr 2 & \quad p \gets A[ 0 ];\ \ell \gets 1 \cr 3 & \quad \textbf{for}\ i \gets 1\ \textbf{to}\ n - 1\ \textbf{do} \quad \triangleright\ \text{unrolled by 4} \cr 4 & \qquad x \gets A[ i ];\ A[ i ] \gets A[ \ell ];\ A[ \ell ] \gets x \cr 5 & \qquad \ell \gets \ell + [ x < p ]\quad \triangleright \ [ \textbf{not}\ p < x ] \text{ when the equal items go left} \cr 6 & \quad \text{exchange } A[ 0 ] \text{ and } A[ \ell - 1 ]; \ \textbf{return}\ \ell - 1 \end{array}} \]
| T ADS::nthElement | ( | std::vector< T > & | A, |
| std::size_t | k, | ||
| C | less = C() ) |
the item of rank \( k \) (from 0) of \( A \), which is rearranged by nthElement() above