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

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). More...

#include "ADS/Contract.h"
#include "ADS/sequence/List.h"
#include "ADS/sort/InsertionSort.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/MergeSort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 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).

When the right part runs out first, the rest of the left one is copied; when the left one does, the rest of the right one is already in place.

  • mergeSort( A , cutoff ) sorts the parts of at most \( \mathit{cutoff} \) items with insertionSort(), which is faster on few items (section 2.8);
  • mergeSortBottomUp() merges the runs of length \( 1 , 2 , 4 , \ldots \) with no recursion and no stack (exercise 12.5);
  • mergeSort( L ) sorts a LIST with pointers in \( O( n \log n ) \) by splitting it in two halves, sorting them and merging them (exercise 12.1), only through the operations of the SEQUENCE: each item is copied once per level of the recursion, since the abstract data type moves no cells from a list to another;
  • countInversions() counts the pairs \( i < j \) with \( A[ i ] > A[ j ] \): when the merge takes \( A[ j ] \) from the right part, it is smaller than all the \( \mathit{mid} - i + 1 \) items left in the left part (exercise 12.3).

The contracts [see Contract.h]: mergeParts() needs \( \mathit{start} \le \mathit{mid} < \mathit{end} < n \), a buffer \( B \) as large as \( A \) and two sorted halves, the latter an audit; mergeSortRec() needs \( \mathit{start} \le \mathit{end} < n \) and \( \mathit{cutoff} \ge 1 \); the sorts end with the same items sorted, an audit. The functions are defined in MergeSort.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 2.7.3, https://algoritmicamente.it/materiali/libro/capitoli/cap02.pdf
D. E. Knuth, The Art of Computer Programming, vol. 3, Sorting and Searching, 2nd ed., Addison-Wesley, 1998, section 5.2.4
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa