|
ADS
Algorithms and Data Structures
|
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: More...
#include "ADS/Contract.h"#include "ADS/sort/Sorted.h"#include <algorithm>#include <cstddef>#include <functional>#include <iterator>#include <type_traits>#include <utility>#include <vector>#include "ADS/sort/impl/RunSorts.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
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 | |
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:
A merge first skips the items of the left run already before the first item of the right one, and those of the right run already after the last item of the left one, both by galloping (an exponential search and then a binary one), and rotates the two runs if the right one comes all before the left one; it then merges with a buffer of the shorter run, from the front if it is the left one and from the back otherwise, so that the buffer, made once, holds half of the items, switching to galloping when one run wins 7 times in a row, as McIlroy proposed, so that two interleaved runs merge in \( O( n ) \) and two runs one after the other in \( O( \log n ) \) comparisons; as in Timsort, the 7 drops by one at each gallop that pays and rises by two at each that does not. On the arithmetic types, two runs that both come from runs extended to \( \mathit{minrun} \), i.e., from an input in disorder whose merge no branch predictor guesses, merge with no branch on the order (a conditional move and two increments); the runs of an input with long runs keep the branches, which follow its patterns, and with the natural order they do not gallop, whose steps cost more than the comparisons that it saves.
Besides its own use, powerSort() is the engine of stableSort() [see Sort.h] for few runs and for the items that no faster route takes, and it stays so also where Boost is found: boost::sort::spinsort is no slower on the benchmark of the sorts, and a program that wants it calls it directly.
The contracts [see Contract.h]: the sort ends with the same items sorted, an audit. The function is defined in RunSorts.tpp.