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

The pattern-defeating quicksort, beyond the book and beyond the quicksorts of Ferragina's book, which are in EngineeredQuickSorts.h [see EngineeredQuickSorts.h, which includes this header]: More...

#include "ADS/Contract.h"
#include "ADS/sort/EngineeredQuickSorts.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/PdqSort.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::pdqSort (Store &&A, C less=C())
 the pattern-defeating quicksort
template<class T, class C = std::less<>>
void ADS::pdqSort (std::vector< T > &A, C less=C())
 pdqSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { pdqSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::pdqSort (R &&A, P &&... p)
 pdqSort() 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 pattern-defeating quicksort, beyond the book and beyond the quicksorts of Ferragina's book, which are in EngineeredQuickSorts.h [see EngineeredQuickSorts.h, which includes this header]:

  • pdqSort(), the pattern-defeating quicksort of Peters: the introsort of Musser in the form of Peters, i.e., the quicksort that allows \( \lfloor \log_2 n \rfloor + 1 \) partitions too unbalanced (see below), past which the part is sorted by heapsort, so that the worst case is \( O( n \log n ) \), with the pivot by the median of three, or by the ninther of Tukey on large parts, and three defences against the patterns: a partition that moved nothing is checked by an insertion sort that gives up after 8 moves, so that a sorted or reversed run takes \( O( n ) \); a pivot equal to the item before the part, i.e., a run of equal keys, gets a partition that puts the equals on its left and skips them, so that \( k \) distinct keys take \( O( n \log k ) \); a partition too unbalanced (one side below \( n / 8 \)) swaps a few items to break the pattern, and too many of them fall back to heapsort. On the arithmetic types with their natural order the partition goes by blocks, as in the BlockQuicksort of Edelkamp and Weiss: the items to swap are first collected, 64 at a time, as offsets by an increment with no branch, and then moved along a cycle, or swapped in pairs when the two blocks have as many (the first with the last, so that a part in reverse order leaves both its sides sorted, which the insertion sort above then finds in \( O( n ) \)), so that no comparison causes a mispredicted branch.

It sorts in place and is not stable; the recursion goes into the left part and loops on the right one, the other partitions leaving at most \( 7 / 8 \) of the items on each side, so that the stack stays \( O( \log n ) \). Besides its own use, pdqSort() is the engine of sort() [see Sort.h] for the short vectors, for few distinct keys and for the items that no faster route takes, and it stays so also where Boost is found: boost::sort::pdqsort 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 functions are defined in PdqSort.tpp.

See also
D. R. Musser, Introspective sorting and selection algorithms, Software: Practice and Experience 27(8), 983-993, 1997, doi:10.1002/(SICI)1097-024X(199708)27:8<983::AID-SPE117>3.0.CO;2-#
O. R. L. Peters, Pattern-defeating quicksort, arXiv:2106.05123, 2021, doi:10.48550/arXiv.2106.05123
J. W. Tukey, The ninther, a technique for low-effort robust (resistant) location in large samples, in H. A. David (ed.), Contributions to Survey Sampling and Applied Statistics, Academic Press, 251-257, 1978, doi:10.1016/B978-0-12-204750-3.50024-1
S. Edelkamp, A. Weiss, BlockQuicksort: avoiding branch mispredictions in quicksort, Proceedings of ESA 2016, LIPIcs 57, article 38, 2016, doi:10.4230/LIPIcs.ESA.2016.38
S. Edelkamp, A. Weiss, BlockQuicksort: avoiding branch mispredictions in quicksort, ACM Journal of Experimental Algorithmics 24, article 1.4, 2019, doi:10.1145/3274660
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa