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

The internal sorts of Knuth that the book leaves out, beyond the book: More...

#include "ADS/Contract.h"
#include "ADS/sort/RadixSort.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <functional>
#include <optional>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/sort/impl/KnuthSorts.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::binaryInsertionSort (Store &&A, C less=C())
 the binary insertion sort
template<class T, class C = std::less<>>
void ADS::binaryInsertionSort (std::vector< T > &A, C less=C())
 binaryInsertionSort() 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::listInsertionSort (Store &&A, C less=C(), std::size_t *moves=nullptr)
 the list insertion sort; the moves of the items go to *moves, if given
template<class T, class C = std::less<>>
void ADS::listInsertionSort (std::vector< T > &A, C less=C(), std::size_t *moves=nullptr)
 listInsertionSort() above on a vector
template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::addressCalculationSort (Store &&A, std::size_t M=0)
 the address calculation sort of numbers into \( M \) lists, \( M = \lfloor n / 4 \rfloor + 1 \) if \( M \) is 0; with an infinite range (an infinity, or a span beyond a double) all the items go to one list, an insertion sort
template<class T>
void ADS::addressCalculationSort (std::vector< T > &A, std::size_t M=0)
 addressCalculationSort() above on a vector
template<RankStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::radixExchangeSort (Store &&A)
 the radix exchange sort
template<RadixKey T>
void ADS::radixExchangeSort (std::vector< T > &A)
 radixExchangeSort() 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::tournamentSort (Store &&A, C less=C())
 the tournament sort
template<class T, class C = std::less<>>
void ADS::tournamentSort (std::vector< T > &A, C less=C())
 tournamentSort() 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::mergeInsertionSort (Store &&A, C less=C())
 the merge insertion sort of Ford and Johnson
template<class T, class C = std::less<>>
void ADS::mergeInsertionSort (std::vector< T > &A, C less=C())
 mergeInsertionSort() above on a vector
long ADS::fordJohnsonBound (std::size_t n)
 the worst case of the comparisons of mergeInsertionSort() on \( n \) items, \( \sum_{k=1}^{n} \lceil \log_2 ( 3 k / 4 ) \rceil \)
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { binaryInsertionSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::binaryInsertionSort (R &&A, P &&... p)
 binaryInsertionSort() 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 ) { listInsertionSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::listInsertionSort (R &&A, P &&... p)
 listInsertionSort() 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 ) { addressCalculationSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::addressCalculationSort (R &&A, P &&... p)
 addressCalculationSort() 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 ) { radixExchangeSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::radixExchangeSort (R &&A, P &&... p)
 radixExchangeSort() 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 ) { tournamentSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::tournamentSort (R &&A, P &&... p)
 tournamentSort() 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 ) { mergeInsertionSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::mergeInsertionSort (R &&A, P &&... p)
 mergeInsertionSort() 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

The internal sorts of Knuth that the book leaves out, beyond the book:

  • binaryInsertionSort() (section 5.2.1 of Knuth) finds the place of each item by a binary search in the sorted prefix, \( \sum_k \lceil \log_2 k \rceil \) comparisons, near the least possible, but still \( \Theta( n^2 ) \) moves; stable;
  • listInsertionSort() (Algorithm 5.2.1L) inserts each item in a list of links, so that no item moves until the end, when the list is followed to rearrange the vector: \( \Theta( n^2 ) \) comparisons and only \( O( n ) \) moves of items; stable;
  • addressCalculationSort() (section 5.2.1, of Isaac and Singleton) inserts each number in one of \( M \) lists by its value, the list \( i \) taking the numbers in the \( i \)-th of \( M \) equal ranges between the least and the largest, as one would file cards under the letters: \( O( n ) \) expected time on numbers spread evenly, with \( M \) about \( n / 4 \); stable;
  • radixExchangeSort() (Algorithm 5.2.2R) is the quicksort on the bits of the keys from the most significant: the part is split into the keys with the bit 0 and those with the bit 1, by exchanges as in a partition, and each half is sorted on the next bit; \( O( n w ) \) for keys of \( w \) bits, not stable;
  • tournamentSort() (section 5.2.3, tree selection) plays a knockout tournament on a complete binary tree of the items, the winner (the least) goes out and only the matches on its path are played again, \( \lceil \log_2 n \rceil \) comparisons each: \( O( n \log n ) \) with \( O( n ) \) extra places, the idea from which heapsort grew; stable, since equal winners are broken by their places;
  • mergeInsertionSort() of Ford and Johnson (section 5.3.1): the items are paired and compared, the larger ones sorted recursively, and the smaller ones inserted by binary search into the chain in the order of the Jacobsthal numbers \( 1 , 3 , 5 , 11 , 21 , \ldots \), so that each insertion searches a prefix of \( 2^k - 1 \) items; it makes \( \sum_{k=1}^n \lceil \log_2 ( 3 k / 4 ) \rceil \) comparisons in the worst case, the fewest of any known method for most \( n \) (and the least possible for \( n \le 11 \) and \( n = 20 , 21 \)), at the price of a bookkeeping that makes it slow in time; not stable.

The contracts [see Contract.h]: each sort ends with the same items sorted, an audit. The functions are defined in KnuthSorts.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, sections 5.2.1, 5.2.2, 5.2.3 and 5.3.1
E. J. Isaac, R. C. Singleton, Sorting by address calculation, Journal of the ACM 3(3), 169-174, 1956, doi:10.1145/320831.320834
P. Hildebrandt, H. Isbitz, Radix exchange: an internal sorting method for digital computers, Journal of the ACM 6(2), 156-163, 1959, doi:10.1145/320964.320972
L. R. Ford, S. M. Johnson, A tournament problem, American Mathematical Monthly 66(5), 387-389, 1959, doi:10.2307/2308750
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa