|
ADS
Algorithms and Data Structures
|
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 | |
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.
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.