|
ADS
Algorithms and Data Structures
|
The sort of \( n \) atomic items in the two-level memory, 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...
Files | |
| file | ADS/external/ExternalSorting.h |
| 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. | |
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, 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.
The definitions, the invariants and the costs of the area are in Theory of external memory.
| void ADS::binaryMergeSort | ( | const DiskFile< T > & | in, |
| DiskFile< T > & | out, | ||
| C | less = C() ) |
the items of in, sorted, into out, by the binary mergesort
In pseudocode (the binary mergesort of chapter 5, Sorting Atomic Items, of Ferragina's book, beyond the book), \( R = \lfloor M / B \rfloor B \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{binaryMergeSort}( \mathit{in} , \mathit{out} ) \cr 2 & \quad \mathcal{R} \gets \text{the runs of } R \text{ items of } \mathit{in} \text{, each read, sorted in memory and written} \cr 3 & \quad \textbf{while}\ |\mathcal{R}| > 1\ \textbf{do} \cr 4 & \qquad \textbf{foreach}\ \text{pair } ( X , Y ) \text{ of } \text{consecutive runs of } \mathcal{R}\ \textbf{do} \cr 5 & \qquad\quad \text{merge } X \text{ and } Y \text{ into a new run, } \text{a block of each in memory} \cr 6 & \qquad \mathcal{R} \gets \text{the new runs, and a lone last run } \text{as it is} \cr 7 & \quad \mathit{out} \gets \text{the only run of } \mathcal{R} \end{array}} \]
| 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
In pseudocode (the multiway quicksort of chapter 5, Sorting Atomic Items, of Ferragina's book, beyond the book), with \( k = \lfloor ( M - B ) / ( 2 B + 1 ) \rfloor \) and \( s = \min\{ \lceil 12 \ln k \rceil k - 1 , ( M - 2 B ) / 2 , n \} \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{distribute}( F , \mathit{out} ) \cr 2 & \quad \textbf{if}\ |F| \le M - B\ \textbf{then}\ \text{read } F \text{, sort it in memory, append it to } \mathit{out}; \ \textbf{return} \cr 3 & \quad \textbf{repeat} \cr 4 & \qquad S \gets s \text{ random items of } F \text{, sorted}; \ p_i \gets S[ i s / k ] \text{ for } 1 \le i < k \text{, without } \text{repetitions} \cr 5 & \qquad \textbf{foreach}\ x \in F\ \textbf{do} \cr 6 & \qquad\quad j \gets \text{the least } j \text{ with } x \le p_j \cr 7 & \qquad\quad \textbf{if}\ x = p_j\ \textbf{then}\ \text{append } x \text{ to } E_j\ \textbf{else}\ \text{append } x \text{ to } F_j \cr 8 & \quad \textbf{until}\ \text{each } F_j \text{ fits in memory or } \text{has fewer than } 4 |F| / k \text{ items, or the third } \text{attempt} \cr 9 & \quad \textbf{foreach}\ j\ \textbf{do}\ \mathrm{distribute}( F_j , \mathit{out} );\ \text{append } E_j \text{ to } \mathit{out} \end{array}} \]
|
inlinenodiscard |
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
| 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]
In pseudocode (the multiway mergesort of chapter 5, Sorting Atomic Items, of Ferragina's book, beyond the book), \( k = \lfloor M / B \rfloor - 1 \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{multiwayMergeSort}( \mathit{in} , \mathit{out} ) \cr 2 & \quad \textbf{if}\ \mathit{snowPlow}\ \textbf{then}\ \mathcal{R} \gets \text{the runs of a replacement selection on a heap of } M - 2 B \text{ items} \cr 3 & \quad \textbf{else}\ \mathcal{R} \gets \text{the runs of } \lfloor M / B \rfloor B \text{ items, each sorted in memory} \cr 4 & \quad \textbf{while}\ |\mathcal{R}| > 1\ \textbf{do} \cr 5 & \qquad \textbf{foreach}\ \text{group } G \text{ of } k \text{ consecutive runs of } \mathcal{R}\ \textbf{do} \cr 6 & \qquad\quad L \gets \text{a tree of losers on the first item of } \text{each run of } G \cr 7 & \qquad\quad \textbf{while}\ L \ne \emptyset\ \textbf{do} \cr 8 & \qquad\qquad j \gets \text{the run of the winner}; \ \text{write } \mathrm{top}( L ) \cr 9 & \qquad\qquad \mathrm{replace}( L , \text{the next item of run } j \text{, or none} ) \cr 10 & \qquad \mathcal{R} \gets \text{the merged runs} \cr 11 & \quad \mathit{out} \gets \text{the only run of } \mathcal{R} \end{array}} \]
| 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
In pseudocode (chapter 5, Sorting Atomic Items, of Ferragina's book, beyond the book), the permutation reduced to two sorts:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{permuteBySorting}( \mathit{in} , \pi , \mathit{out} ) \cr 2 & \quad P \gets \text{the triples } \langle i , \pi[ i ] , \cdot \rangle \text{, by a scan of } \pi \cr 3 & \quad \text{sort } P \text{ by the second component} \cr 4 & \quad \text{fill the third component of the } i \text{-th triple } \text{of } P \text{ with } \mathit{in}[ i ] \text{, by a coordinated } \text{scan} \cr 5 & \quad \text{sort } P \text{ by the first component} \cr 6 & \quad \mathit{out} \gets \text{the third components of } P \text{, by a scan} \end{array}} \]
| 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
In pseudocode (the direct permutation of chapter 5, Sorting Atomic Items, of Ferragina's book, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{permuteDirect}( \mathit{in} , \pi , \mathit{out} ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 1\ \textbf{do} \cr 3 & \qquad \text{append } \mathit{in}[ \pi[ i ] ] \text{ to } \mathit{out}\quad \triangleright\ \text{a read unless its block is } \text{in the buffer} \end{array}} \]
|
inlinenodiscard |
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! \)
|
inlinenodiscard |
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 \)
The reads are the smallest \( t \) with \( t \log_2 \binom{M}{B} \ge \log_2 n! - \lceil n / B \rceil \log_2 B! \), the logarithms of the factorials computed by the function \( \Gamma \).