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

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

Detailed description

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 run is a maximal non-decreasing stretch, or a strictly decreasing one that is reversed (strictly, so that the reversal keeps the sort stable); a run shorter than \( \mathit{minrun} = 24 \) items is extended to \( \mathit{minrun} \) items by a binary insertion sort, so that the merges start from runs long enough;
  • powerSort() of Munro and Wild (the sort of Python from 3.11) gives each boundary between two runs a power, the first bit where the midpoints of the two runs, as fractions of \( n \), differ, and merges the runs on the stack while their power is larger than that of the new boundary: the merges then follow a nearly optimal tree of merges, at most \( n H + 2 n \) comparisons for \( H \) the entropy of the lengths of the runs, which is \( O( n ) \) on few runs and \( O( n \log n ) \) always.

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.

See also
T. Peters, listsort.txt, the documentation of the sort of CPython, 2002
J. I. Munro, S. Wild, Nearly-optimal mergesorts: fast, practical sorting methods that optimally adapt to existing runs, Proceedings of ESA 2018, LIPIcs 112, article 63, 2018, doi:10.4230/LIPIcs.ESA.2018.63
P. M. McIlroy, Optimistic sorting and information theoretic complexity, Proceedings of the 4th ACM-SIAM Symposium on Discrete Algorithms (SODA), 467-474, 1993
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa