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

The quicksorts beyond the quicksort of Montresor's book [see QuickSort.h for it and its improvements of section 12.2]: the two of chapter 5, Sorting Atomic Items, of Ferragina's book, and the one engineered for speed, pdqSort(), which is in PdqSort.h [see PdqSort.h, which this header includes]: More...

#include "ADS/Contract.h"
#include "ADS/sort/InsertionSort.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/sort/impl/EngineeredQuickSorts.tpp"
#include "ADS/sort/PdqSort.h"

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::dualPivotQuickSort (Store &&A, C less=C())
 the dual-pivot quicksort
template<class T, class C = std::less<>>
void ADS::dualPivotQuickSort (std::vector< T > &A, C less=C())
 dualPivotQuickSort() above on a vector
template<SortStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::threeWayQuickSort (Store &&A, C less=C())
 the quicksort with the three-way partition of Ferragina's book
template<class T, class C = std::less<>>
void ADS::threeWayQuickSort (std::vector< T > &A, C less=C())
 threeWayQuickSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { dualPivotQuickSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::dualPivotQuickSort (R &&A, P &&... p)
 dualPivotQuickSort() 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 ) { threeWayQuickSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::threeWayQuickSort (R &&A, P &&... p)
 threeWayQuickSort() 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 quicksorts beyond the quicksort of Montresor's book [see QuickSort.h for it and its improvements of section 12.2]: the two of chapter 5, Sorting Atomic Items, of Ferragina's book, and the one engineered for speed, pdqSort(), which is in PdqSort.h [see PdqSort.h, which this header includes]:

  • dualPivotQuickSort() of Yaroslavskiy (the sort of the primitive types of Java 7), as section 5.3.5 of Ferragina's book presents it: two pivots \( p \le q \), the 2nd and the 4th of five equally spaced items, split the part into three, smaller than \( p \), between and larger than \( q \), in one pass; with two random items as pivots it makes 5% fewer comparisons and 80% more swaps than the classical one on average (\( 1.9 n \ln n \) and \( 0.6 n \ln n \), against \( 2 n \ln n \) and \( n \ln n / 3 \)), as Wild and Nebel computed, and fewer scans of the memory;
  • threeWayQuickSort(), the quicksort of section 5.3 of Ferragina's book with the three-way partition of its Algorithm 5.4 (section 5.3.1): three pointers move rightward, and the items equal to the pivot gather in a middle part that no later call sorts again, so that \( k \) distinct keys cost \( O( n \log k ) \) on average; the pivot is drawn at random, as section 5.3.2 suggests, by a generator of fixed seed (the same sort on every run), and the recursion is bounded as in its Algorithm 5.6. It makes \( 2 n \ln n \) comparisons on average (Theorem 5.5 of the book), at most two per item and partition, but \( \Theta( n^2 ) \) in the worst case, when the pivots drawn are the smallest or the largest items of their parts, which happens with a probability that vanishes with \( n \) on every input, there being no fallback to heapsort.

Both sort in place and are not stable; the recursion goes into the smaller part and loops on the larger, so that the stack stays \( O( \log n ) \).

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

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapter 5, Sorting Atomic Items, doi:10.1017/9781009128933
V. Yaroslavskiy, Dual-pivot quicksort, 2009
S. Wild, M. E. Nebel, Average case analysis of Java 7's dual pivot quicksort, Proceedings of ESA 2012, LNCS 7501, 825-836, 2012, doi:10.1007/978-3-642-33090-2_71
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa