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

The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort, the one-digit cases, in CountingSort.h and PigeonholeSort.h): More...

#include "ADS/Contract.h"
#include "ADS/parallel/Scratch.h"
#include "ADS/sort/EngineeredQuickSorts.h"
#include "ADS/sort/InsertionSort.h"
#include "ADS/sort/Sorted.h"
#include "ADS/sort/VectorQuickSort.h"
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <functional>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/sort/impl/RadixSort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Concepts

concept  ADS::RadixKey
 an integer or a floating point number of at most 8 bytes

Functions

template<RadixKey T>
auto ADS::radixKey (T x)
 the unsigned key of \( x \), whose order is the order of the numbers: the bits of \( x \) with the sign bit flipped for a signed integer, and for a floating point number all of them flipped if it is negative and the sign bit otherwise
template<SortStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::radixSort (Store &&A)
 the LSD radix sort of numbers
template<RadixKey T>
void ADS::radixSort (std::vector< T > &A)
 radixSort() above on a vector
template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class K>
void ADS::radixSortBy (Store &&A, K key)
 the LSD radix sort of any items by the unsigned key( x ), stable
template<class T, class K>
void ADS::radixSortBy (std::vector< T > &A, K key)
 radixSortBy() above on a vector
template<SortStore Store, RadixKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > >>
void ADS::americanFlagSort (Store &&A)
 the MSD radix sort of numbers in place
template<RadixKey T>
void ADS::americanFlagSort (std::vector< T > &A)
 americanFlagSort() above on a vector
template<StringStore Store>
void ADS::msdRadixSort (Store &&A)
 the MSD radix sort of strings
void ADS::msdRadixSort (std::vector< std::string > &A)
 msdRadixSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { radixSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::radixSort (R &&A, P &&... p)
 radixSort() 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
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { radixSortBy( v , std::forward< P >( p )... ); }
decltype(auto) ADS::radixSortBy (R &&A, P &&... p)
 radixSortBy() 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
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { americanFlagSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::americanFlagSort (R &&A, P &&... p)
 americanFlagSort() 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
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { msdRadixSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::msdRadixSort (R &&A, P &&... p)
 msdRadixSort() 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

Variables

constexpr std::size_t ADS::kFlagSmall = kVectorSort ? 8192 : 256
 the groups that americanFlagSort() leaves to a sort by comparisons are those of fewer items than this: many with the vector registers, whose quicksort beats the radix passes that it replaces, few otherwise

Detailed description

The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort, the one-digit cases, in CountingSort.h and PigeonholeSort.h):

  • radixSort() sorts integers and floating point numbers by their bytes, from the least significant (LSD), each pass a stable Counting Sort on one byte between the vector and a buffer: \( O( w ( n + 256 ) ) \) for keys of \( w \) bytes, no comparison. The buffer of trivial items is the block that ScratchRoom [see Scratch.h] keeps between the calls, so that a sort does not pay the faults of fresh pages. The key of a number is its bits made unsigned so that their order is the order of the numbers: the sign bit flipped for a signed integer, and for a floating point number all the bits flipped if it is negative and the sign bit otherwise (the order of IEEE 754, \( -0 \) before \( +0 \), the NaN at the ends). A first sweep finds the bytes in which some keys differ, and only those are counted (all the histograms in a second sweep) and make a pass, so that small keys in wide types cost only the bytes they use; when they differ in one byte alone, the key tells the number, and a counting sort writes the numbers back from one of each value, with no buffer. A vector larger than the cache (1 MiB), whose keys differ in 3 bytes or more, first goes by its top byte into the buffer, the one pass that scatters over the whole memory, and each part is sorted in the same way and then by the other bytes while it fits, so that their passes stay in the cache (an MSD pass before the LSD ones);
  • radixSortBy() is the same on any items, by an unsigned key that a function gives, stable, e.g., the records by one field;
  • americanFlagSort() is the radix sort from the most significant byte (MSD), in place: two scans that stop at the first pair of keys out of their order find a vector already sorted or in reverse order (reversed), and a sweep the bytes in which some keys differ (one alone: the counting sort of radixSort()); then the items are permuted into the 256 buckets of the first byte that differs, as the flag of the name divides into stripes, and each bucket is sorted in the same way by the next one. The permutation does not follow its cycles, whose every step waits for the load of the step before: each item of the part of a bucket not yet in place is swapped to the next free place of its own bucket, and the buckets are taken in turn until all are full, so that the loads of a round are independent (Skarupke). A bucket of fewer than kFlagSmall items is left to vectorQuickSort() [see VectorQuickSort.h], whose order on the floating point numbers is the one of the keys, or, without the vector registers, to pdqSort() [see EngineeredQuickSorts.h]: \( O( w n ) \) for keys of \( w \) bytes, \( O( 1 ) \) extra space per level of the recursion and no buffer of the items, not stable;
  • msdRadixSort() sorts strings by their characters from the first one, the end of a string before every character: the first pass permutes them into their buckets in place, as the American flag sort does, and then each bucket has a buffer, made only if some bucket is not all of equal strings, between which and the vector the strings go back and forth, one move per pass, the character of each string read once per pass and kept aside, and the small groups sorted by insertion, so that it reads only the distinguishing prefixes of the strings: \( O( D + n ) \) time for \( D \) the total length of those prefixes; the prefix that all the strings of a group share is first skipped by comparing them with one of them (one memcmp() for a string that keeps the whole prefix), so that a group of equal strings, or of strings with a long common prefix, costs one comparison instead of one pass per character.

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

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 2.7.5 (Counting Sort) and exercise 4.11 (Pigeonhole Sort), https://algoritmicamente.it/materiali/libro/capitoli/cap02.pdf
P. M. McIlroy, K. Bostic, M. D. McIlroy, Engineering radix sort, Computing Systems 6(1), 5-27, 1993
T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, section 8.3
M. Herf, Radix tricks, 2001, http://stereopsis.com/radix.html (the key of a floating point number, the histograms in one sweep)
M. Skarupke, I wrote a faster sorting algorithm, 2016, (the permutation by rounds of independent swaps), https://probablydance.com/2016/12/27/i-wrote-a-faster-sorting-algorithm/
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa