|
ADS
Algorithms and Data Structures
|
Quicksort (section 12.2): partition(), the perno() of the book, rearranges \( A[ \mathit{start} .. \mathit{end} ] \) around the pivot \( x = A[ \mathit{start} ] \), so that the items before its final place \( j \) are smaller and the ones after it are not, and the two parts are sorted recursively. More...
#include "ADS/Contract.h"#include "ADS/sequence/List.h"#include "ADS/sort/InsertionSort.h"#include "ADS/sort/Sorted.h"#include <algorithm>#include <cstddef>#include <functional>#include <tuple>#include <utility>#include <vector>#include "ADS/sort/impl/QuickSort.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<>> | |
| std::size_t | ADS::partition (Store &&A, std::size_t start, std::size_t end, C less=C()) |
| the pivot \( A[ \mathit{start} ] \) moved to its place \( j \), the smaller items before it, the others after it (the perno() of the book); \( \mathit{end} - \mathit{start} \) comparisons | |
| template<class T, class C = std::less<>> | |
| std::size_t | ADS::partition (std::vector< T > &A, std::size_t start, std::size_t end, C less=C()) |
| partition() above on a vector | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| std::size_t | ADS::partitionByValue (Store &&A, std::size_t start, std::size_t end, const T &x, C less=C()) |
| the partition of \( A[ \mathit{start} .. \mathit{end} ] \) around a value \( x \) that need not be in it, in one pass from both ends: the items not larger than \( x \) first, the larger ones after; the number \( r \) of the items not larger than \( x \) | |
| template<class T, class C = std::less<>> | |
| std::size_t | ADS::partitionByValue (std::vector< T > &A, std::size_t start, std::size_t end, const T &x, C less=C()) |
| partitionByValue() 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::quickSort (Store &&A, std::size_t start, std::size_t end, C less=C()) |
| the sort of \( A[ \mathit{start} .. \mathit{end} ] \), the QuickSort() of the book | |
| template<class T, class C = std::less<>> | |
| void | ADS::quickSort (std::vector< T > &A, std::size_t start, std::size_t end, C less=C()) |
| quickSort() 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::quickSort (Store &&A, C less=C()) |
| \( A \) in increasing order by less, the Quicksort of the whole vector | |
| template<class T, class C = std::less<>> | |
| void | ADS::quickSort (std::vector< T > &A, C less=C()) |
| quickSort() 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::quickSortTuned (Store &&A, std::size_t cutoff=16, C less=C(), std::size_t *depth=nullptr) |
| Quicksort with the improvements of section 12.2; the largest number of parts on the stack goes to *depth, if given. | |
| template<class T, class C = std::less<>> | |
| void | ADS::quickSortTuned (std::vector< T > &A, std::size_t cutoff=16, C less=C(), std::size_t *depth=nullptr) |
| quickSortTuned() above on a vector | |
| template<class T, class C = std::less<>> | |
| void | ADS::quickSort (List< T > &L, C less=C()) |
| Quicksort of a list with pointers: the items go to three lists, smaller, equal and larger than the first one, the first and the last are sorted and the three are joined. | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { quickSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::quickSort (R &&A, P &&... p) |
| quickSort() 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 ) { quickSortTuned( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::quickSortTuned (R &&A, P &&... p) |
| quickSortTuned() 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 | |
Quicksort (section 12.2): partition(), the perno() of the book, rearranges \( A[ \mathit{start} .. \mathit{end} ] \) around the pivot \( x = A[ \mathit{start} ] \), so that the items before its final place \( j \) are smaller and the ones after it are not, and the two parts are sorted recursively.
With \( n - 1 \) comparisons per partition,
\[ T( n ) = n - 1 + \frac{2}{n} \sum_{j=1}^{n-1} T( j ) \leq 2 ( n + 1 ) \sum_{k=3}^{n+1} \frac{1}{k} = O( n \log n ) \]
on average over the permutations, but \( n ( n - 1 ) / 2 \) when the pivot is always the smallest item, e.g., on a sorted vector, and also when all the items are equal (exercise 12.4), and then the recursion of quickSort() and of quickSort( L ) is \( n - 1 \) calls deep, which the stack of the program must hold. Not stable.
quickSortTuned() combines the improvements of the section: the pivot is the median of \( A[ \mathit{start} ] \), \( A[ \lfloor ( \mathit{start} + \mathit{end} ) / 2 \rfloor ] \) and \( A[ \mathit{end} ] \); the partition of Hoare scans from both ends, swapping the pairs on the wrong sides, so that equal items split evenly and the case of all equal items takes \( O( n \log n ) \); the parts of at most \( \mathit{cutoff} \) items are left to insertionSort(); the recursion is an explicit stack that holds the larger part while the smaller one is sorted, so that it never holds more than \( \lfloor \log_2 n \rfloor \) parts. quickSort( L ) sorts a LIST with pointers by moving its items into three lists, smaller, equal and larger than the pivot (exercise 12.1).
The contracts [see Contract.h]: partition() needs \( \mathit{start} \le \mathit{end} < n \) and ends with the pivot in its place, the smaller items before it and the others after it, an audit; quickSort() on a range needs \( \mathit{end} < n \) unless the range is empty; the sorts end with the same items sorted, an audit. The functions are defined in QuickSort.tpp.