ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Selection.h

The selection of section 17.1. More...

Files

file  ADS/search/Selection.h
 The selection of section 17.1: the item of rank \( k \), the \( k \)-th smallest, \( 1 \le k \le n \), the median for \( k = \lceil n / 2 \rceil \), without sorting.

Functions

template<class T, class R = std::mt19937>
T ADS::selection (std::vector< T > &A, std::size_t k, R *rng=nullptr)
 the item of rank \( k \), \( 1 \le k \le n \), of \( A \) (the selezione() of the book); with a generator the pivots are random, otherwise the first item of the part
template<class T>
T ADS::medianOfMedians (std::vector< T > &A, std::size_t k)
 the item of rank \( k \), \( 1 \le k \le n \), in linear time in the worst case

Detailed description

The selection of section 17.1.

The definitions, the invariants and the costs of the area are in Theory of searching.

Function documentation

◆ medianOfMedians()

template<class T>
T ADS::medianOfMedians ( std::vector< T > & A,
std::size_t k )

the item of rank \( k \), \( 1 \le k \le n \), in linear time in the worst case

In pseudocode (Blum, Floyd, Pratt, Rivest and Tarjan, beyond the book), with a partition in three ways:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( A , \mathit{lo} , \mathit{hi} , r ) \cr 2 & \quad \textbf{if}\ \mathit{hi} - \mathit{lo} + 1 \le 10\ \textbf{then} \cr 3 & \qquad \text{sort } A[ \mathit{lo} .. \mathit{hi} ] \text{ by } \text{insertion};\ \textbf{return}\ A[ \mathit{lo} + r - 1 ] \cr 4 & \quad \text{sort each group of 5 and move its median to the front, } \text{to } A[ \mathit{lo} .. m - 1 ] \cr 5 & \quad x \gets \mathrm{select}( A , \mathit{lo} , m - 1 , \lceil ( m - \mathit{lo} ) / 2 \rceil ) \cr 6 & \quad \text{split } A[ \mathit{lo} .. \mathit{hi} ] \text{ into } < x \text{, } = x \text{ and } > x \text{, of sizes } a \text{, } b \text{ and } c \cr 7 & \quad \textbf{if}\ r \le a\ \textbf{then return}\ \mathrm{select}( \text{the part } < x , r ) \cr 8 & \quad \textbf{else if}\ r \le a + b\ \textbf{then return}\ x \cr 9 & \quad \textbf{else return}\ \mathrm{select}( \text{the part } > x , r - a - b ) \cr 10 & \textbf{function}\ \mathrm{medianOfMedians}( A , k ) \cr 11 & \quad \textbf{return}\ \mathrm{select}( A , 0 , n - 1 , k ) \end{array}} \]

Precondition
\( 1 \le k \le n \)
Postcondition
the result has rank \( k \) in \( A \) (audit)

◆ selection()

template<class T, class R = std::mt19937>
T ADS::selection ( std::vector< T > & A,
std::size_t k,
R * rng = nullptr )

the item of rank \( k \), \( 1 \le k \le n \), of \( A \) (the selezione() of the book); with a generator the pivots are random, otherwise the first item of the part

In pseudocode (section 17.1), the tail recursion of the book as a loop:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{selection}( A , k ) \cr 2 & \quad ( \mathit{start} , \mathit{end} ) \gets ( 0 , n - 1 )\quad \triangleright\ \text{the } k \text{-th of } A[ \mathit{start} .. \mathit{end} ] \cr 3 & \quad \textbf{while}\ \mathit{start} < \mathit{end}\ \textbf{do} \cr 4 & \qquad A[ \mathit{start} ] \leftrightarrow A[ r ] \text{ for an } r \text{ drawn in } [ \mathit{start} , \mathit{end} ] \cr 5 & \qquad j \gets \mathrm{partition}( A , \mathit{start} , \mathit{end} );\ q \gets j - \mathit{start} + 1\quad \triangleright\ \text{the rank} \cr 6 & \qquad \textbf{if}\ k < q\ \textbf{then}\ \mathit{end} \gets j - 1 \cr 7 & \qquad \textbf{else if}\ k > q\ \textbf{then}\ ( \mathit{start} , k ) \gets ( j + 1 , k - q ) \cr 8 & \qquad \textbf{else return}\ A[ j ] \cr 9 & \quad \textbf{return}\ A[ \mathit{start} ] \end{array}} \]

Precondition
\( 1 \le k \le n \)
Postcondition
the result has rank \( k \) in \( A \) (audit)