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

The natural merge sort that adapts to the runs of the input, beyond the book, stable. More...

Files

file  ADS/sort/RunSorts.h
 The natural merge sort that adapts to the runs of the input, beyond the book [see naturalMergeSort() of SequenceAlgorithms.h for the plain one], stable:

Functions

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::powerSort (Store &&A, C less=C(), std::size_t *runs=nullptr)
 Powersort; the number of runs found goes to *runs, if given.
template<class T, class C = std::less<>>
void ADS::powerSort (std::vector< T > &A, C less=C(), std::size_t *runs=nullptr)
 powerSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { powerSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::powerSort (R &&A, P &&... p)
 powerSort() 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 natural merge sort that adapts to the runs of the input, beyond the book, stable.

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

Function documentation

◆ powerSort() [1/3]

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

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

◆ powerSort() [2/3]

template<class T, class C = std::less<>>
void ADS::powerSort ( std::vector< T > & A,
C less = C(),
std::size_t * runs = nullptr )

powerSort() above on a vector

◆ powerSort() [3/3]

template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::powerSort ( Store && A,
C less = C(),
std::size_t * runs = nullptr )

Powersort; the number of runs found goes to *runs, if given.

In pseudocode (Munro and Wild, beyond the book), the power of the boundary between two adjacent runs \( [ s_1 , s_2 ) \) and \( [ s_2 , e_2 ) \) being the least \( p \) such that \( ( s_1 + s_2 ) / 2 n \) and \( ( s_2 + e_2 ) / 2 n \) differ in their \( p \)-th bit after the point:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{powerSort}( A , n ) \cr 2 & \quad S \gets \text{an empty stack};\ [ s , e ) \gets \text{the first } \text{run} \cr 3 & \quad \textbf{while}\ e < n\ \textbf{do} \cr 4 & \qquad [ e , e_2 ) \gets \text{the next run};\ p \gets \text{the } \text{power of the boundary } e \cr 5 & \qquad \textbf{while}\ S \ne \emptyset\ \textbf{and}\ \text{the power } \text{of } \mathrm{top}( S ) > p\ \textbf{do} \cr 6 & \qquad\quad [ s' , s ) \gets \mathrm{pop}( S );\ \text{merge } [ s' , s ) \text{ and } [ s , e );\ s \gets s' \cr 7 & \qquad \mathrm{push}( S , ( [ s , e ) , p ) );\ [ s , e ) \gets [ e , e_2 ) \cr 8 & \quad \textbf{while}\ S \ne \emptyset\ \textbf{do}\ \text{merge } \mathrm{pop}( S ) \text{ with } [ s , e ) \end{array}} \]

where a run is ascending, or strictly descending and reversed, and is extended to \( \mathit{minrun} \) items by binary insertion if shorter. The power is found with no fraction, on the doubled midpoints, one bit at a time:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{nodePower}( s_1 , s_2 , e_2 , n ) \cr 2 & \quad l \gets s_1 + s_2;\ r \gets s_2 + e_2;\ k \gets 0 \cr 3 & \quad \textbf{while}\ [ l \ge n ] = [ r \ge n ]\ \textbf{do}\quad \triangleright\ \text{the same } ( k + 1 ) \text{-th bit} \cr 4 & \qquad k \gets k + 1;\ \textbf{if}\ l \ge n\ \textbf{then} \ l \gets l - n;\ r \gets r - n \cr 5 & \qquad l \gets 2 l;\ r \gets 2 r \cr 6 & \quad \textbf{return}\ k + 1 \end{array}} \]

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