|
ADS
Algorithms and Data Structures
|
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. More...
#include "ADS/Contract.h"#include <cmath>#include <cstddef>#include <functional>#include <iterator>#include <memory>#include <utility>#include <vector>#include "ADS/search/impl/NthElement.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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 [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.
The pivot is chosen by the sampling of Floyd and Rivest: on a range of \( n > 600 \) items and a rank \( k \) (from 0), a sample of the places from
\[ \ell = \max\Big( 0 , k - \frac{k s}{n} + \delta \Big) \quad \mbox{to} \quad r = \min\Big( n - 1 , k + \frac{( n - k ) s}{n} + \delta \Big) , \qquad s = \frac{1}{2} n^{2/3} , \quad \delta = \pm \frac{1}{2} \sqrt{ \frac{ s ( n - s ) \ln n }{ n } } \]
(the sign of \( \delta \) toward the median) is selected recursively for the rank \( k \), so that the item that lands in the place \( k \) falls just on the side of \( k \) that leaves the smaller part to recur on; the expected number of comparisons is \( n + \min( k , n - k ) + O( n^{2/3} ( \ln n )^{1/3} ) \) (Kiwiel), against the \( 2 n + 2 k \ln( n / k ) + 2 ( n - k ) \ln( n / ( n - k ) ) + o( n ) \) of a pivot at random (up to \( 2 ( 1 + \ln 2 ) n \approx 3.39 n \) for the median). Below 600 items the pivot is the median of three.
The partition suits the order of the range, which 64 items taken at a stride tell: on a random order (more than 16 turns, a rise followed by a fall or the opposite, among them; about \( 2 \cdot 62 / 3 \approx 41 \) on a random permutation) it is the scheme of Lomuto with no branch, an item moved to the end of the smaller ones and the item there to its place, the end advancing by the outcome of the comparison (an addition rather than a jump, so that no comparison is mispredicted, where the partition of Hoare mispredicts half of them on random items, some 10 cycles each), unrolled by 4 so that the loop is not at the mercy of the alignment of one jump; on a range in some order (an organ pipe, a few runs) it is the scheme of Hoare, whose branches are then predicted and which writes only the items on the wrong side, and the sample of Floyd and Rivest is first gathered from the whole range at a stride, since a contiguous one would not represent it. A range whose 64 items rise all (or fall all) is checked to be sorted, by blocks of 64 pairs with no exit inside a block, and is then left as it is (or reversed in the same pass of the check), \( n \) comparisons in all. When the pivot is the smallest item of the range, the items equal to it go to the left side, so that many equal items end in one step; the items of a range cut on its left by a pivot are not smaller than it, and a later pivot equal to it is known to be the smallest with no pass.
Each round charges the items of its part to a budget of \( 4 n \) items, which a random order hardly ever exhausts; a round whose part the rest of the budget does not cover takes as its pivot the median of the medians of the groups of 5, which has at least \( 3 n / 10 \) items on each side, and when the part on its right is larger than \( 7 n / 10 + 2 \), which happens only if many items are equal to it, a second partition takes them away too, so that what is left costs \( T( n ) \le T( n / 5 ) + T( 7 n / 10 ) + O( n ) = O( n ) \). The other rounds partition at most \( 4 n \) items in all, and their samples, selected recursively within their own budgets, hold \( O( m^{2/3} ) \) items of a part of \( m > 600 \), less than \( m / 16 \), so that by induction the selection takes \( O( n ) \) time in the worst case. A range of at most 24 items is sorted by insertion.
The contracts [see Contract.h]: nth in \( [ \mathit{first} , \mathit{last} ) \); the items before nth are not larger than it and those after it not smaller, an audit. The function is defined in NthElement.tpp.