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

Merge Sort (section 2.7.3). More...

Files

file  ADS/sort/MergeSort.h
 Merge Sort (section 2.7.3): the two halves of the vector are sorted recursively and merged by mergeParts(), the Merge() of the book, through a vector \( B \) allocated once, \( T( n ) = 2 T( n / 2 ) + \Theta( n ) = \Theta( n \log n ) \) time, optimal among the sorts by comparisons, \( \Theta( n ) \) extra space, stable (an item of the left part goes first when equal).

Functions

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeParts (Store &&A, std::vector< T > &B, std::size_t start, std::size_t mid, std::size_t end, C less=C(), long long *inv=nullptr)
 \( A[ \mathit{start} .. \mathit{mid} ] \) and \( A[ \mathit{mid} + 1 .. \mathit{end} ] \), sorted, merged through \( B \) (the Merge() of the book); the inversions met are added to *inv, if given
template<class T, class C = std::less<>>
void ADS::mergeParts (std::vector< T > &A, std::vector< T > &B, std::size_t start, std::size_t mid, std::size_t end, C less=C(), long long *inv=nullptr)
 mergeParts() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeSortRec (Store &&A, std::vector< T > &B, std::size_t start, std::size_t end, std::size_t cutoff, C less=C(), long long *inv=nullptr)
 the sort of \( A[ \mathit{start} .. \mathit{end} ] \) (the MergeSort() of the book), insertionSort() on at most \( \mathit{cutoff} \) items; with \( \mathit{cutoff} > 1 \) also no merge of two parts already in order, i.e., with \( A[ \mathit{mid} ] \le A[ \mathit{mid} + 1 ] \), which makes the sort of a sorted vector take \( n - 1 \) comparisons
template<class T, class C = std::less<>>
void ADS::mergeSortRec (std::vector< T > &A, std::vector< T > &B, std::size_t start, std::size_t end, std::size_t cutoff, C less=C(), long long *inv=nullptr)
 mergeSortRec() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeSort (Store &&A, std::size_t cutoff=1, C less=C())
 \( A \) in increasing order, stable; the parts of at most \( \mathit{cutoff} \) items by insertionSort() (1 is the book)
template<class T, class C = std::less<>>
void ADS::mergeSort (std::vector< T > &A, std::size_t cutoff=1, C less=C())
 mergeSort() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeSortBottomUp (Store &&A, C less=C())
 the iterative Merge Sort: the runs of length \( w \) are merged in pairs, for \( w = 1 , 2 , 4 , \ldots \)
template<class T, class C = std::less<>>
void ADS::mergeSortBottomUp (std::vector< T > &A, C less=C())
 mergeSortBottomUp() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
long long ADS::countInversions (Store &&A, C less=C())
 the number of inversions of \( A \), the pairs \( i < j \) with \( A[ j ] < A[ i ] \), by Merge Sort, \( O( n \log n ) \); \( A \) ends sorted
template<class T, class C = std::less<>>
long long ADS::countInversions (std::vector< T > &A, C less=C())
 countInversions() above on a vector
template<class T, class C = std::less<>>
void ADS::mergeSort (List< T > &L, C less=C())
 Merge Sort of a list with pointers, through the operations of the SEQUENCE alone: the items are copied from a list to another, once per level, since the abstract data type moves no cells.
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { mergeSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::mergeSort (R &&A, P &&... p)
 mergeSort() 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, on its view otherwise, since the sort reads the items by their ranks only
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { mergeSortBottomUp( v , std::forward< P >( p )... ); }
decltype(auto) ADS::mergeSortBottomUp (R &&A, P &&... p)
 mergeSortBottomUp() 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, on its view otherwise, since the sort reads the items by their ranks only
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { countInversions( v , std::forward< P >( p )... ); }
decltype(auto) ADS::countInversions (R &&A, P &&... p)
 countInversions() 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, on its view otherwise, since the sort reads the items by their ranks only

Detailed description

Merge Sort (section 2.7.3).

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

Function documentation

◆ countInversions() [1/3]

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

countInversions() 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, on its view otherwise, since the sort reads the items by their ranks only

◆ countInversions() [2/3]

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

countInversions() above on a vector

◆ countInversions() [3/3]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
long long ADS::countInversions ( Store && A,
C less = C() )

the number of inversions of \( A \), the pairs \( i < j \) with \( A[ j ] < A[ i ] \), by Merge Sort, \( O( n \log n ) \); \( A \) ends sorted

In pseudocode (exercise 12.3), each merge counting the items of the left part that an item of the right part overtakes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{countInversions}( A ) \cr 2 & \quad \mathit{inv} \gets 0 \cr 3 & \quad \textbf{if}\ n \ge 2\ \textbf{then}\ \mathrm{mergeSortRec}( A , B , 0 , n - 1 , 1 )\quad \triangleright\ \mathrm{mergeParts} \text{ adds} \cr 4 & \quad \textbf{return}\ \mathit{inv} \end{array}} \]

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

◆ mergeParts() [1/2]

template<class T, class C = std::less<>>
void ADS::mergeParts ( std::vector< T > & A,
std::vector< T > & B,
std::size_t start,
std::size_t mid,
std::size_t end,
C less = C(),
long long * inv = nullptr )

mergeParts() above on a vector

◆ mergeParts() [2/2]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeParts ( Store && A,
std::vector< T > & B,
std::size_t start,
std::size_t mid,
std::size_t end,
C less = C(),
long long * inv = nullptr )

\( A[ \mathit{start} .. \mathit{mid} ] \) and \( A[ \mathit{mid} + 1 .. \mathit{end} ] \), sorted, merged through \( B \) (the Merge() of the book); the inversions met are added to *inv, if given

In pseudocode (section 2.7.3), the smaller head moving to \( B \), the left one on a tie:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeParts}( A , B , \mathit{start} , \mathit{mid} , \mathit{end} ) \cr 2 & \quad i \gets \mathit{start};\ j \gets \mathit{mid} + 1;\ k \gets \mathit{start} \cr 3 & \quad \textbf{while}\ i \le \mathit{mid}\ \textbf{and}\ j \le \mathit{end}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ A[ j ] < A[ i ]\ \textbf{then}\quad \triangleright \ A[ j ] \text{ passes } A[ i .. \mathit{mid} ] \cr 5 & \qquad\quad \mathit{inv} \gets \mathit{inv} + \mathit{mid} - i + 1; \ B[ k ] \gets A[ j ];\ j \gets j + 1 \cr 6 & \qquad \textbf{else}\ B[ k ] \gets A[ i ];\ i \gets i + 1 \cr 7 & \qquad k \gets k + 1 \cr 8 & \quad \text{append } A[ i .. \mathit{mid} ] \text{ to } B \text{ from } B[ k ] \text{, advancing } k \cr 9 & \quad \text{copy } B[ \mathit{start} .. k - 1 ] \text{ back to } A \quad \triangleright\ A[ k .. \mathit{end} ] \text{ is in place} \end{array}} \]

Precondition
\( \mathit{start} \le \mathit{mid} < \mathit{end} < n \le |B| \), \( A[ \mathit{start} .. \mathit{mid} ] \) and \( A[ \mathit{mid} + 1 .. \mathit{end} ] \) sorted (audit)
Postcondition
\( A[ \mathit{start} .. \mathit{end} ] \) sorted (audit)

◆ mergeSort() [1/4]

template<class T, class C = std::less<>>
void ADS::mergeSort ( List< T > & L,
C less = C() )

Merge Sort of a list with pointers, through the operations of the SEQUENCE alone: the items are copied from a list to another, once per level, since the abstract data type moves no cells.

In pseudocode (exercise 12.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeSort}( L ) \cr 2 & \quad n \gets \text{the length of } L;\ \textbf{if}\ n < 2 \ \textbf{then return} \cr 3 & \quad R \gets \text{an empty list} \cr 4 & \quad \text{move the items of } L \text{ after its first } \lfloor n / 2 \rfloor \text{ to the end of } R \cr 5 & \quad \mathrm{mergeSort}( L );\ \mathrm{mergeSort}( R );\ q \gets L.\mathrm{head}() \cr 6 & \quad \textbf{foreach}\ x \in R \text{ from its head}\ \textbf{do} \cr 7 & \qquad \textbf{while}\ \textbf{not}\ L.\mathrm{finished}( q ) \ \textbf{and}\ \textbf{not}\ x < L.\mathrm{read}( q )\ \textbf{do} \cr 8 & \qquad\quad q \gets L.\mathrm{next}( q ) \cr 9 & \qquad L.\mathrm{insert}( q , x )\quad \triangleright\ \text{before the first larger item} \end{array}} \]

Postcondition
\( L \) is sorted (audit)

◆ mergeSort() [2/4]

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

mergeSort() 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, on its view otherwise, since the sort reads the items by their ranks only

◆ mergeSort() [3/4]

template<class T, class C = std::less<>>
void ADS::mergeSort ( std::vector< T > & A,
std::size_t cutoff = 1,
C less = C() )

mergeSort() above on a vector

◆ mergeSort() [4/4]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeSort ( Store && A,
std::size_t cutoff = 1,
C less = C() )

\( A \) in increasing order, stable; the parts of at most \( \mathit{cutoff} \) items by insertionSort() (1 is the book)

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeSort}( A , \mathit{cutoff} ) \cr 2 & \quad \textbf{if}\ n \ge 2\ \textbf{then}\ \mathrm{mergeSortRec}( A , \text{a vector of } n \text{ places} , 0 , n - 1 , \mathit{cutoff} ) \end{array}} \]

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

◆ mergeSortBottomUp() [1/3]

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

mergeSortBottomUp() 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, on its view otherwise, since the sort reads the items by their ranks only

◆ mergeSortBottomUp() [2/3]

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

mergeSortBottomUp() above on a vector

◆ mergeSortBottomUp() [3/3]

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

the iterative Merge Sort: the runs of length \( w \) are merged in pairs, for \( w = 1 , 2 , 4 , \ldots \)

In pseudocode (exercise 12.5), after the pass of width \( w \) each run of \( 2 w \) items from a multiple of \( 2 w \) being sorted:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeSortBottomUp}( A , n ) \cr 2 & \quad \textbf{for}\ w \gets 1 , 2 , 4 , \ldots \text{ while } w < n \ \textbf{do}\quad \triangleright\ \text{the runs of length } w \text{ sorted} \cr 3 & \qquad \textbf{for}\ \mathit{start} \gets 0 , 2 w , 4 w , \ldots \text{ while } \mathit{start} + w < n\ \textbf{do} \cr 4 & \qquad\quad \mathrm{mergeParts}( A , B , \mathit{start} , \mathit{start} + w - 1 , \min\{ \mathit{start} + 2 w , n \} - 1 ) \end{array}} \]

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

◆ mergeSortRec() [1/2]

template<class T, class C = std::less<>>
void ADS::mergeSortRec ( std::vector< T > & A,
std::vector< T > & B,
std::size_t start,
std::size_t end,
std::size_t cutoff,
C less = C(),
long long * inv = nullptr )

mergeSortRec() above on a vector

◆ mergeSortRec() [2/2]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::mergeSortRec ( Store && A,
std::vector< T > & B,
std::size_t start,
std::size_t end,
std::size_t cutoff,
C less = C(),
long long * inv = nullptr )

the sort of \( A[ \mathit{start} .. \mathit{end} ] \) (the MergeSort() of the book), insertionSort() on at most \( \mathit{cutoff} \) items; with \( \mathit{cutoff} > 1 \) also no merge of two parts already in order, i.e., with \( A[ \mathit{mid} ] \le A[ \mathit{mid} + 1 ] \), which makes the sort of a sorted vector take \( n - 1 \) comparisons

In pseudocode (section 2.7.3, with the cutoff of section 2.8):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{mergeSortRec}( A , B , \mathit{start} , \mathit{end} , \mathit{cutoff} ) \cr 2 & \quad \textbf{if}\ \mathit{end} - \mathit{start} + 1 \le \mathit{cutoff}\ \textbf{then} \cr 3 & \qquad \mathrm{insertionSort}( A , \mathit{start} , \mathit{end} + 1 );\ \textbf{return} \cr 4 & \quad \mathit{mid} \gets \mathit{start} + \lfloor ( \mathit{end} - \mathit{start} ) / 2 \rfloor \cr 5 & \quad \mathrm{mergeSortRec}( A , B , \mathit{start} , \mathit{mid} , \mathit{cutoff} ) \cr 6 & \quad \mathrm{mergeSortRec}( A , B , \mathit{mid} + 1 , \mathit{end} , \mathit{cutoff} ) \cr 7 & \quad \textbf{if}\ \mathit{cutoff} = 1\ \textbf{or}\ A[ \mathit{mid} + 1 ] < A[ \mathit{mid} ]\ \textbf{then} \cr 8 & \qquad \mathrm{mergeParts}( A , B , \mathit{start} , \mathit{mid} , \mathit{end} )\quad \triangleright\ \text{else already in order} \end{array}} \]

Precondition
\( \mathit{start} \le \mathit{end} < n \le |B| \) and \( \mathit{cutoff} \ge 1 \)