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

The quicksorts beyond the quicksort of Montresor's book. More...

Files

file  ADS/sort/EngineeredQuickSorts.h
 The quicksorts beyond the quicksort of Montresor's book [see QuickSort.h for it and its improvements of section 12.2]: the two of chapter 5, Sorting Atomic Items, of Ferragina's book, and the one engineered for speed, pdqSort(), which is in PdqSort.h [see PdqSort.h, which this header includes]:

Functions

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::dualPivotQuickSort (Store &&A, C less=C())
 the dual-pivot quicksort
template<class T, class C = std::less<>>
void ADS::dualPivotQuickSort (std::vector< T > &A, C less=C())
 dualPivotQuickSort() 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::threeWayQuickSort (Store &&A, C less=C())
 the quicksort with the three-way partition of Ferragina's book
template<class T, class C = std::less<>>
void ADS::threeWayQuickSort (std::vector< T > &A, C less=C())
 threeWayQuickSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { dualPivotQuickSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::dualPivotQuickSort (R &&A, P &&... p)
 dualPivotQuickSort() 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 ) { threeWayQuickSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::threeWayQuickSort (R &&A, P &&... p)
 threeWayQuickSort() 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 quicksorts beyond the quicksort of Montresor's book.

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

Function documentation

◆ dualPivotQuickSort() [1/3]

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

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

◆ dualPivotQuickSort() [2/3]

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

dualPivotQuickSort() above on a vector

◆ dualPivotQuickSort() [3/3]

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

the dual-pivot quicksort

In pseudocode (section 5.3.5 of Ferragina's book, Yaroslavskiy, with the pivots of Java 7), \( A[ \mathit{lo} + 1 .. l - 1 ] < p \), \( A[ g + 1 .. \mathit{hi} - 2 ] > q \) and the items between in \( [ p , q ] \) at all times:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{dual}( A , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad \textbf{if}\ \mathit{hi} - \mathit{lo} \le 27\ \textbf{then} \ \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} ); \ \textbf{return} \cr 3 & \quad p , q \gets \text{the 2nd and 4th of five spaced items, to } A[ \mathit{lo} ] \text{ and } A[ \mathit{hi} - 1 ] \cr 4 & \quad l \gets \mathit{lo} + 1;\ g \gets \mathit{hi} - 2;\ k \gets l \cr 5 & \quad \textbf{while}\ k \le g\ \textbf{do} \cr 6 & \qquad \textbf{if}\ A[ k ] < p\ \textbf{then}\ A[ k ] \leftrightarrow A[ l ];\ l \gets l + 1 \cr 7 & \qquad \textbf{else if}\ A[ k ] \ge q\ \textbf{then} \cr 8 & \qquad\quad \textbf{while}\ A[ g ] > q\ \textbf{and}\ k < g \ \textbf{do}\ g \gets g - 1 \cr 9 & \qquad\quad A[ k ] \leftrightarrow A[ g ];\ g \gets g - 1 \cr 10 & \qquad\quad \textbf{if}\ A[ k ] < p\ \textbf{then}\ A[ k ] \leftrightarrow A[ l ];\ l \gets l + 1 \cr 11 & \qquad k \gets k + 1 \cr 12 & \quad \text{put } p \text{ in } A[ l - 1 ] \text{ and } q \text{ in } \text{} A[ g + 1 ] \cr 13 & \quad \text{sort the three parts, the middle one only if } p < q \end{array}} \]

where the two smaller parts are sorted recursively and the largest by the loop.

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

◆ threeWayQuickSort() [1/3]

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

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

◆ threeWayQuickSort() [2/3]

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

threeWayQuickSort() above on a vector

◆ threeWayQuickSort() [3/3]

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

the quicksort with the three-way partition of Ferragina's book

In pseudocode (Algorithms 5.4 and 5.6 of chapter 5, Sorting Atomic Items, of Ferragina's book, with \( n_0 = 16 \)), the pivot \( P \) in \( A[ \mathit{lo} ] \) and, while \( c \) scans, \( A[ \mathit{lo} .. l - 1 ] < P \), \( A[ l .. r - 1 ] = P \) and \( A[ r .. c - 1 ] > P \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{partition}( A , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad P \gets A[ \mathit{lo} ];\ l \gets \mathit{lo};\ r \gets \mathit{lo} + 1 \cr 3 & \quad \textbf{for}\ c \gets r\ \textbf{to}\ \mathit{hi} - 1 \ \textbf{do} \cr 4 & \qquad \textbf{if}\ A[ c ] = P\ \textbf{then}\ A[ c ] \leftrightarrow A[ r ];\ r \gets r + 1 \cr 5 & \qquad \textbf{else if}\ A[ c ] < P\ \textbf{then}\ A[ c ] \leftrightarrow A[ l ];\ A[ c ] \leftrightarrow A[ r ];\ r \gets r + 1;\ l \gets l + 1 \cr 6 & \quad \textbf{return}\ ( l , r ) \cr 7 & \textbf{procedure}\ \mathrm{threeWay}( A , \mathit{lo} , \mathit{hi} ) \cr 8 & \quad \textbf{while}\ \mathit{hi} - \mathit{lo} > n_0\ \textbf{do} \cr 9 & \qquad A[ \mathit{lo} ] \leftrightarrow A[ x ] \text{ for } x \text{ } \text{drawn at random in } [ \mathit{lo} , \mathit{hi} ) \cr 10 & \qquad ( l , r ) \gets \mathrm{partition}( A , \mathit{lo} , \mathit{hi} ) \cr 11 & \qquad \textbf{if}\ l - \mathit{lo} \le \mathit{hi} - r\ \textbf{then} \ \mathrm{threeWay}( A , \mathit{lo} , l );\ \mathit{lo} \gets r \cr 12 & \qquad \textbf{else}\ \mathrm{threeWay}( A , r , \mathit{hi} ); \ \mathit{hi} \gets l \cr 13 & \quad \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} ) \end{array}} \]

where \( A[ c ] = P \) means that neither is smaller than the other; \( \Theta( n^2 ) \) comparisons in the worst case.

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