ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ExternalSorting.h File Reference

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

Detailed description

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.

  • binaryMergeSort() cuts the input in runs of \( R = \lfloor M / B \rfloor B \) items, sorted in memory (the recursion of the mergesort stopped where the subsequence fits, which saves its \( \log_2 M \) lowest levels), and then merges the runs two by two, three blocks of memory; each pass reads and writes every block once, hence at most \( 2 \lceil n / B \rceil ( 1 + \lceil \log_2 ( n / R ) \rceil ) \) I/Os, mergeSortIOs() with \( k = 2 \).
  • multiwayMergeSort() merges \( k = M / B - 1 \) runs at a time, a block per run and one for the output, the minimum chosen by a tree of losers [see LoserTree.h] or by any structure with its interface (a parameter, so that the same algorithm runs on the trees and the heaps of ADS and of the other libraries, with the same I/Os), so that the passes drop to \( \lceil \log_k ( n / R ) \rceil \) and the I/Os to \( O( ( n / B ) \log_{M / B} ( n / M ) ) \), which is optimal by the lower bound below (chapter 5, Sorting Atomic Items, of Ferragina's book). With snowPlow the runs are made by replacement selection, the snow plow of Knuth [see replacementSelection() of ExternalSort.h, the same runs in memory], a heap of \( M - 2 B \) items that gives runs of \( 2 ( M - 2 B ) \) items on average on a random input and a single run on a sorted one.
  • sortingLowerBound() is the bound of Aggarwal and Vitter by the tree of the I/Os: a read of \( B \) items into a memory that holds \( M - B \) others can have at most \( \binom{M}{B} \) outcomes, times \( B! \) when the \( B \) items are read for the first time, and the leaves must be at least \( n! \); the smallest \( t \) with \( \binom{M}{B}^t ( B! )^{\lceil n / B \rceil} \ge n! \) bounds the reads, \( t = \Omega( ( n / B ) \log_{M / B} ( n / M ) ) \), and the output adds its \( \lceil n / B \rceil \) writes. permutingLowerBound() counts also the writes, each of which can go to \( n + 1 \) places: \( ( ( n + 1 ) \binom{M}{B} )^t ( B! )^{\lceil n / B \rceil} \ge n! \), which gives \( \Omega( \min\{ n , ( n / B ) \log_{M / B} ( n / M ) \} ) \): moving the items, not finding their order, is the cost of a sort on the disk.
  • permuteBySorting() reaches that bound by sorting: the pairs \( \langle i , \pi( i ) \rangle \) sorted by \( \pi( i ) \), a coordinated scan with the input that fills in the items, a sort back by \( i \), two sorts and three scans; permuteDirect() moves one item per read, up to \( n \) reads, better only when \( B \) is below the logarithm of the sort.
  • distributionSort() is the multiway quicksort of the same chapter: \( k - 1 \) pivots taken from a sample of \( ( a + 1 ) k - 1 \) random items, \( a + 1 = \lceil 12 \ln k \rceil \) (with probability at least \( 1 / 2 \) every bucket gets fewer than \( 4 n / k \) items, else the sample is drawn again, at most twice), the items distributed by one scan into the buckets between the pivots and into one bucket per pivot for the items equal to it (the three-way partition of quicksort, so that equal keys stop the recursion), and the buckets sorted in turn, in memory once they fit. The memory holds the input block, the \( 2 k - 1 \) blocks of the buckets, the block of the output and the pivots, hence \( k = \lfloor ( M - B ) / ( 2 B + 1 ) \rfloor \), half the fan-in of the merge, and the same \( O( ( n / B ) \log_{M / B} ( n / M ) ) \) I/Os on average.

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.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 5, Sorting Atomic Items, doi:10.1017/9781009128933
A. Aggarwal, J. S. Vitter, The input/output complexity of sorting and related problems, Communications of the ACM 31(9), 1116-1127, 1988, doi:10.1145/48529.48535
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, sections 5.4.1 and 5.4.9
W. D. Frazer, A. C. McKellar, Samplesort: a sampling approach to minimal storage tree sorting, Journal of the ACM 17(3), 496-507, 1970, doi:10.1145/321592.321600
J. S. Vitter, External memory algorithms and data structures: dealing with massive data, ACM Computing Surveys 33(2), 209-271, 2001, doi:10.1145/384192.384193
M. H. Nodine, J. S. Vitter, Greed Sort: optimal deterministic sorting on parallel disks, Journal of the ACM 42(4), 919-933, 1995, doi:10.1145/210332.210343
R. Dementiev, L. Kettner, P. Sanders, STXXL: standard template library for XXL data sets, Software: Practice and Experience 38(6), 589-637, 2008, doi:10.1002/spe.844
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa