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

The multikey quicksort of Bentley and Sedgewick, as chapter 7, Sorting Strings, of Ferragina's book presents it: the quicksort of strings by one character at a time, a partition into three parts, the strings whose character \( d \) is smaller, equal and larger than that of the pivot, the equal part going on with the character \( d + 1 \) and the others with \( d \). More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <cstddef>
#include <functional>
#include <string>
#include <utility>
#include <vector>
#include "ADS/sort/impl/MultikeyQuickSort.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<StringStore Store>
void ADS::multikeyQuickSort (Store &&A)
 the multikey quicksort of strings
void ADS::multikeyQuickSort (std::vector< std::string > &A)
 multikeyQuickSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { multikeyQuickSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::multikeyQuickSort (R &&A, P &&... p)
 multikeyQuickSort() 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 multikey quicksort of Bentley and Sedgewick, as chapter 7, Sorting Strings, of Ferragina's book presents it: the quicksort of strings by one character at a time, a partition into three parts, the strings whose character \( d \) is smaller, equal and larger than that of the pivot, the equal part going on with the character \( d + 1 \) and the others with \( d \).

It is the sort that corresponds to the ternary search tree of the same paper; its pivot is the first string of the part. It makes at most \( ( \sigma + 2 ) ( D + n ) \) comparisons of characters, \( D \) the total length of the distinguishing prefixes and \( \sigma \) the size of the alphabet, and \( O( D + n \log n ) \) in expectation on a random order of distinct strings, against the \( O( n \log n ) \) comparisons of whole strings of a quicksort, each as long as their common prefix. It sorts in place, not stable.

The contracts [see Contract.h]: the vector ends with the same strings sorted, an audit. The function is defined in MultikeyQuickSort.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 7, Sorting Strings, doi:10.1017/9781009128933
J. L. Bentley, R. Sedgewick, Fast algorithms for sorting and searching strings, Proceedings of the 8th ACM-SIAM Symposium on Discrete Algorithms (SODA), 360-369, 1997
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa