ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SampleSort.h

The in-place super scalar samplesort, beyond the book. More...

Files

file  ADS/sort/SampleSort.h
 The in-place super scalar samplesort, beyond the book: sampleSort() splits the vector into up to 256 buckets by splitters drawn from a sample, in place with \( O( k b ) \) extra items for \( k \) buckets and blocks of \( b \) items (about 2 KiB), and sorts each bucket in the same way, those of at most 64 items by insertion.

Functions

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::sampleSort (Store &&A, C less=C())
 the in-place super scalar samplesort of \( A \) by less
template<class T, class C = std::less<>>
void ADS::sampleSort (std::vector< T > &A, C less=C())
 sampleSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { sampleSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::sampleSort (R &&A, P &&... p)
 sampleSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses

Detailed description

The in-place super scalar samplesort, beyond the book.

The definitions, the invariants and the costs of the area are in Theory of sorting.

Function documentation

◆ sampleSort() [1/3]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { sampleSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::sampleSort ( R && A,
P &&... p )

sampleSort() on the range \( A \) [see SortRange], the other arguments as for a vector: in place on the span of its items if \( A \) is contiguous, through a vector of its items otherwise, since the code of the sort reads the items by their addresses

◆ sampleSort() [2/3]

template<class T, class C = std::less<>>
void ADS::sampleSort ( std::vector< T > & A,
C less = C() )

sampleSort() above on a vector

◆ sampleSort() [3/3]

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::sampleSort ( Store && A,
C less = C() )

the in-place super scalar samplesort of \( A \) by less

In pseudocode (Sanders and Winkel, and Axtmann et al. for the work in place, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sampleSort}( A , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad \textbf{if}\ \mathit{hi} - \mathit{lo} \le 64\ \textbf{then} \ \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} ); \ \textbf{return} \cr 3 & \quad \text{draw and sort a sample};\ t \gets \text{the tree of its } k - 1 \text{ splitters} \cr 4 & \quad \text{classify each item } x \text{ into a buffer of its bucket,} \text{ by } j \gets 2 j + [ t_j \le x ] \text{ from } j = 1 \cr 5 & \quad \text{flush each full buffer as a block to the front of } A[ \mathit{lo} .. \mathit{hi} - 1 ] \cr 6 & \quad \text{permute the blocks into the areas of their buckets} \cr 7 & \quad \text{move the buffers and the overflow into the gaps of the } \text{buckets} \cr 8 & \quad \textbf{foreach}\ \text{bucket } [ b , e ) \text{ not of items } \text{equal to a splitter}\ \textbf{do} \cr 9 & \qquad \textbf{if}\ e - b > ( \mathit{hi} - \mathit{lo} ) / 2 \ \textbf{then}\ \mathrm{pdqSort}( A , b , e )\ \textbf{else} \ \mathrm{sampleSort}( A , b , e ) \end{array}} \]

Postcondition
\( A \) is sorted, with the same items (audit)