|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include "ADS/sort/InsertionSort.h"#include "ADS/sort/QuickSort.h"#include <algorithm>#include <cstddef>#include <random>#include <utility>#include <vector>#include "ADS/search/impl/Selection.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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 | |
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.
\[ T( n ) \le n - 1 + \frac{2}{n} \sum_{q = \lfloor n / 2 \rfloor}^{n-1} T( q ) \le c n , \]
linear for every input;Both rearrange \( A \).
The contracts [see Contract.h]: \( 1 \le k \le n \), and the item returned has rank \( k \), i.e., fewer than \( k \) items are smaller and at least \( k \) are not larger, an audit in \( O( n ) \). The functions are defined in Selection.tpp.