|
ADS
Algorithms and Data Structures
|
The sort of \( n \) atomic items in the two-level memory [see TwoLevelMemory.h], by merging and by distribution, with the lower bounds that make both optimal, written as functions that a test can compare with the I/Os counted. More...
#include "ADS/Contract.h"#include "ADS/external/TwoLevelMemory.h"#include "ADS/numeric/RandomNumbers.h"#include "ADS/priority/DaryHeap.h"#include "ADS/priority/LoserTree.h"#include "ADS/search/BinarySearch.h"#include "ADS/sort/ExternalSort.h"#include "ADS/sort/Sort.h"#include "ADS/sort/Sorted.h"#include <algorithm>#include <cmath>#include <cstddef>#include <cstdint>#include <functional>#include <memory>#include <optional>#include <utility>#include <vector>#include "ADS/external/impl/ExternalSorting.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| std::uint64_t | ADS::mergeSortIOs (std::size_t n, std::size_t M, std::size_t B, std::size_t k) |
| the I/Os of a mergesort of fan-in at most \( k \), \( 2 \lceil n / B \rceil ( 1 + \lceil \log_k \lceil n / R \rceil \rceil )
\) with \( R = \lfloor M / B \rfloor B \): a pass that forms the runs and one per level of the merges, each reading and writing every block | |
| std::uint64_t | ADS::sortingLowerBound (std::size_t n, std::size_t M, std::size_t B) |
| the least I/Os of a sort of \( n \) items by comparisons, \( \lceil n / B \rceil \) writes plus the reads that the tree of the I/Os needs, never fewer than \( \lceil n / B \rceil \) | |
| std::uint64_t | ADS::permutingLowerBound (std::size_t n, std::size_t M, std::size_t B) |
| the least I/Os of a permutation of \( n \) items, the larger of \( 2 \lceil n / B \rceil \) and the smallest \( t \) with \( t ( \log_2 ( n + 1 ) + \log_2 \binom{M}{B} ) \ge \log_2 n! -
\lceil n / B \rceil \log_2 B! \) | |
| template<class T, class C = std::less<>> | |
| void | ADS::binaryMergeSort (const DiskFile< T > &in, DiskFile< T > &out, C less=C()) |
| the items of in, sorted, into out, by the binary mergesort | |
| template<class T, class C = std::less<>, class Sel = LoserTree< T , C >> | |
| void | ADS::multiwayMergeSort (const DiskFile< T > &in, DiskFile< T > &out, C less=C(), bool snowPlow=false) |
| the items of in, sorted, into out, by the mergesort of fan-in \( M / B - 1 \); the runs by replacement selection if snowPlow; the least of the heads of the runs chosen by a Sel, a tree of losers by default [see LoserTree.h for its interface] | |
| template<class T, class R, class C = std::less<>> | |
| void | ADS::distributionSort (const DiskFile< T > &in, DiskFile< T > &out, R &g, C less=C()) |
| the items of in, sorted, into out, by the distribution with pivots from a random sample drawn by g | |
| template<class T> | |
| void | ADS::permuteBySorting (const DiskFile< T > &in, const DiskFile< std::size_t > &pi, DiskFile< T > &out) |
| \( \mathit{out}[ i ] = \mathit{in}[ \pi[ i ] ] \), by two sorts and three scans | |
| template<class T> | |
| void | ADS::permuteDirect (const DiskFile< T > &in, const DiskFile< std::size_t > &pi, DiskFile< T > &out) |
| \( \mathit{out}[ i ] = \mathit{in}[ \pi[ i ] ] \), one read per item at most | |
The sort of \( n \) atomic items in the two-level memory [see TwoLevelMemory.h], by merging and by distribution, with the lower bounds that make both optimal, written as functions that a test can compare with the I/Os counted.
Compared with the literature: the merge with a tree of losers is the standard of the external sorters (section 5.4.1 of Knuth, the STXXL library of Dementiev, Kettner and Sanders), the distribution with oversampling the sample sort of Frazer and McKellar, here as in the analysis of Vitter's survey; on \( D \) disks the striping turns either into a sorter with blocks of \( D B \) items, \( O( ( n / D B ) \log_{M / D B} ( n / M ) ) \), which loses a factor \( 1 - \log_{M / B} D \) in the base of the logarithm, and Greed Sort of Nodine and Vitter recovers the optimum (not simulated here: one disk is the model of these functions). The items of the model are atomic: a pair or a triple counts as one item.
The contracts [see Contract.h]: \( M \ge 3 B \) for the merges and \( M \ge 5 B + 2 \) for the distribution; the files share the memory, the output file is empty; the output holds the items of the input, sorted, an audit; \( \pi \) is a permutation of \( 0 , \ldots , n - 1 \), an audit. The functions are defined in ExternalSorting.tpp.