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

The sort that chooses its algorithm, beyond the book. More...

Files

file  ADS/sort/Sort.h
 The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort and pick, among the sorts of ADS, the one that suits it.

Functions

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::sort (Store &&A, C less=C())
 the sort chosen for \( A \) and less, not necessarily stable
template<class T, class C = std::less<>>
void ADS::sort (std::vector< T > &A, C less=C())
 sort() above on a vector
template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::stableSort (Store &&A, C less=C())
 the stable sort chosen for \( A \) and less
template<class T, class C = std::less<>>
void ADS::stableSort (std::vector< T > &A, C less=C())
 stableSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { sort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::sort (R &&A, P &&... p)
 sort() 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
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { stableSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::stableSort (R &&A, P &&... p)
 stableSort() 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

Variables

constexpr std::size_t ADS::kRadix = 512
 the items from which the numbers go to radixSort()
constexpr std::size_t ADS::kStrings = 64
 the items from which the strings go to the sorts by characters
constexpr std::size_t ADS::kRuns = 16
 the runs up to which the numbers and the strings go to powerSort()

Detailed description

The sort that chooses its algorithm, beyond the book.

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

Function documentation

◆ sort() [1/3]

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

sort() 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

◆ sort() [2/3]

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

sort() above on a vector

◆ sort() [3/3]

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

the sort chosen for \( A \) and less, not necessarily stable

In pseudocode (beyond the book), \( r \) the runs of \( A \), counted up to \( \mathtt{kRuns} + 1 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{sort}( A ) \cr 2 & \quad \textbf{if}\ \text{numbers of 32 or 64 bits, natural order, } \text{vector registers}\ \textbf{then} \cr 3 & \qquad \text{merge the runs if } r \le \mathtt{kRuns} \text{, else } \mathrm{vectorQuickSort}( A ) \cr 4 & \quad \textbf{else if}\ \text{other numbers, natural order} \ \textbf{then} \cr 5 & \qquad \textbf{if}\ n < \mathtt{kRadix}\ \textbf{then} \ \mathrm{pdqSort}( A ) \cr 6 & \qquad \textbf{else if}\ r \le \mathtt{kRuns}\ \textbf{then} \ \text{merge the runs as } \mathrm{powerSort}() \cr 7 & \qquad \textbf{else if}\ \text{a sample shows few keys wide apart} \ \textbf{then}\ \mathrm{pdqSort}( A ) \cr 8 & \qquad \textbf{else}\ \mathrm{radixSort}( A ) \cr 9 & \quad \textbf{else if}\ \text{strings, natural order}\ \textbf{then} \cr 10 & \qquad \textbf{if}\ n < \mathtt{kStrings}\ \textbf{then} \ \mathrm{pdqSort}( A ) \cr 11 & \qquad \textbf{else if}\ r \le \mathtt{kRuns}\ \textbf{then} \ \text{merge the runs as } \mathrm{powerSort}() \cr 12 & \qquad \textbf{else if}\ \text{vector registers and not few distinct } \text{strings}\ \textbf{then}\ \mathrm{prefixKeySort}( A ) \cr 13 & \qquad \textbf{else}\ \mathrm{msdRadixSort}( A ) \cr 14 & \quad \textbf{else if}\ \text{trivially copyable items}\ \textbf{then} \cr 15 & \qquad \textbf{if}\ n < \mathtt{kRadix}\ \textbf{then} \ \mathrm{pdqSort}( A ) \cr 16 & \qquad \textbf{else if}\ r \le \mathtt{kRuns}\ \textbf{then} \ \text{merge the runs as } \mathrm{powerSort}() \cr 17 & \qquad \textbf{else}\ \mathrm{sampleSort}( A ) \cr 18 & \quad \textbf{else if}\ n < \mathtt{kRadix}\ \textbf{then} \ \mathrm{pdqSort}( A )\ \textbf{else}\ \mathrm{sampleSort}( A ) \end{array}} \]

where one run is reversed if decreasing and left as it is otherwise.

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

◆ stableSort() [1/3]

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

stableSort() 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

◆ stableSort() [2/3]

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

stableSort() above on a vector

◆ stableSort() [3/3]

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

the stable sort chosen for \( A \) and less

In pseudocode (beyond the book), \( r \) the runs of \( A \) as in sort():

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{stableSort}( A ) \cr 2 & \quad \textbf{if}\ \text{numbers, natural order}\ \textbf{then} \cr 3 & \qquad \text{as } \mathrm{sort}( A ) \text{, but } \mathrm{powerSort}( A ) \text{ for the short vectors and } -0 = +0 \cr 4 & \quad \textbf{else if}\ \text{strings, natural order}\ \textbf{then} \cr 5 & \qquad \text{as } \mathrm{sort}( A ) \text{, with } \mathrm{powerSort}( A ) \text{ for the short vectors} \cr 6 & \quad \textbf{else if}\ \text{trivially copyable items}\ \textbf{then} \cr 7 & \qquad \textbf{if}\ r \le \mathtt{kRuns}\ \textbf{then}\ \text{merge } \text{the runs as } \mathrm{powerSort}() \cr 8 & \qquad \textbf{else if}\ n \ge \mathtt{kRadix} \text{ and a sample } \text{shows at most 16 keys}\ \textbf{then}\ \text{count by them} \cr 9 & \qquad \textbf{else}\ \text{merge sort through a buffer of } n / 2 \text{ items} \cr 10 & \quad \textbf{else}\ \mathrm{powerSort}( A ) \end{array}} \]

Postcondition
\( A \) is sorted, with the same items, the equal ones in their order (audit)

Variable documentation

◆ kRadix

std::size_t ADS::kRadix = 512
inlineconstexpr

the items from which the numbers go to radixSort()

◆ kRuns

std::size_t ADS::kRuns = 16
inlineconstexpr

the runs up to which the numbers and the strings go to powerSort()

◆ kStrings

std::size_t ADS::kStrings = 64
inlineconstexpr

the items from which the strings go to the sorts by characters