ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
QuickSort.h

Quicksort (section 12.2). More...

Files

file  ADS/sort/QuickSort.h
 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.

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

Detailed description

Quicksort (section 12.2).

The definitions, the invariants and the costs of the area are in Theory of sorting.

Function documentation

◆ partition() [1/2]

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

◆ partition() [2/2]

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

In pseudocode (section 12.2), \( A[ \mathit{start} + 1 .. j ] \) holding the items smaller than \( x \) seen so far:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{partition}( A , \mathit{start} , \mathit{end} ) \cr 2 & \quad x \gets A[ \mathit{start} ];\ j \gets \mathit{start}\quad \triangleright\ A[ \mathit{start} + 1 .. j ] < x \cr 3 & \quad \textbf{for}\ i \gets \mathit{start} + 1\ \textbf{to} \ \mathit{end}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ A[ i ] < x\ \textbf{then}\ j \gets j + 1;\ A[ j ] \leftrightarrow A[ i ] \cr 5 & \quad A[ \mathit{start} ] \gets A[ j ];\ A[ j ] \gets x\quad \triangleright\ \text{the pivot between the two parts} \cr 6 & \quad \textbf{return}\ j \end{array}} \]

Precondition
\( \mathit{start} \le \mathit{end} < n \)
Postcondition
\( A[ \mathit{start} .. j - 1 ] < A[ j ] \) and \( A[ j ] \le A[ j + 1 .. \mathit{end} ] \) (audit)

◆ partitionByValue() [1/2]

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

◆ partitionByValue() [2/2]

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 \)

In pseudocode (the partition of Hoare, section 12.2), with \( A[ \mathit{start} .. i - 1 ] \le x < A[ j .. \mathit{end} ] \) at all times:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{partitionByValue}( A , \mathit{start} , \mathit{end} , x ) \cr 2 & \quad i \gets \mathit{start};\ j \gets \mathit{end} + 1\quad \triangleright\ A[ \mathit{start} .. i - 1 ] \le x < A[ j .. \mathit{end} ] \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{while}\ i < j\ \textbf{and}\ \textbf{not}\ x < A[ i ] \ \textbf{do}\ i \gets i + 1 \cr 5 & \qquad \textbf{while}\ i < j\ \textbf{and}\ x < A[ j - 1 ] \ \textbf{do}\ j \gets j - 1 \cr 6 & \qquad \textbf{if}\ i + 1 \ge j\ \textbf{then return}\ i - \mathit{start}\quad \triangleright\ \text{the items} \le x \cr 7 & \qquad j \gets j - 1;\ A[ i ] \leftrightarrow A[ j ];\ i \gets i + 1 \end{array}} \]

Precondition
\( \mathit{start} \le \mathit{end} < n \)
Postcondition
\( A[ \mathit{start} .. \mathit{start} + r - 1 ] \le x \) and \( x < A[ \mathit{start} + r .. \mathit{end} ] \), the same items (audit)

◆ quickSort() [1/6]

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.

In pseudocode (exercise 12.1):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{quickSort}( L ) \cr 2 & \quad \textbf{if}\ L \text{ has at most one item}\ \textbf{then return} \cr 3 & \quad x \gets \text{the item at the head of } L;\ S , E , G \gets \text{three empty lists} \cr 4 & \quad \textbf{while}\ L \text{ is not empty}\ \textbf{do} \cr 5 & \qquad \text{remove the head } y \text{ of } L \text{ and append it} \cr & \qquad\quad \text{to } S \text{ if } y < x \text{, to } G \text{ if } x < y \text{, to } E \text{ otherwise} \cr 6 & \quad \mathrm{quickSort}( S );\ \mathrm{quickSort}( G ) \cr 7 & \quad \text{append to } L \text{ the items of } S \text{, then } E \text{, then } G \end{array}} \]

Postcondition
\( L \) is sorted (audit)

◆ quickSort() [2/6]

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

◆ quickSort() [3/6]

template<class T, class C = std::less<>>
void ADS::quickSort ( std::vector< T > & A,
C less = C() )

quickSort() above on a vector

◆ quickSort() [4/6]

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

◆ quickSort() [5/6]

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

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{quickSort}( A ) \cr 2 & \quad \textbf{if}\ n > 1\ \textbf{then}\ \mathrm{quickSort}( A , 0 , n - 1 ) \end{array}} \]

Postcondition
\( A \) is sorted, with the same items (audit)

◆ quickSort() [6/6]

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

In pseudocode (section 12.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{quickSort}( A , \mathit{start} , \mathit{end} ) \cr 2 & \quad \textbf{if}\ \mathit{start} \ge \mathit{end}\ \textbf{then return} \cr 3 & \quad j \gets \mathrm{partition}( A , \mathit{start} , \mathit{end} )\quad \triangleright\ \text{the pivot is now in } A[ j ] \cr 4 & \quad \mathrm{quickSort}( A , \mathit{start} , j - 1 ); \ \mathrm{quickSort}( A , j + 1 , \mathit{end} ) \end{array}} \]

Precondition
\( \mathit{start} > \mathit{end} \) or \( \mathit{end} < n \)
Postcondition
\( A[ \mathit{start} .. \mathit{end} ] \) is sorted

◆ quickSortTuned() [1/3]

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

◆ quickSortTuned() [2/3]

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

◆ quickSortTuned() [3/3]

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.

In pseudocode (section 12.2), the median of three placed in \( A[ \mathit{hi} - 1 ] \), so that \( A[ \mathit{lo} ] \le x \) and \( A[ \mathit{hi} - 1 ] = x \) stop the two scans:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{quickSortTuned}( A , \mathit{cutoff} ) \cr 2 & \quad S \gets \text{an empty stack of intervals};\ ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , n - 1 ) \cr 3 & \quad \textbf{loop} \cr 4 & \qquad \textbf{if}\ \mathit{hi} - \mathit{lo} + 1 \le \mathit{cutoff} \ \textbf{then} \cr 5 & \qquad\quad \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} + 1 ) \cr 6 & \qquad\quad \textbf{if}\ S = \emptyset\ \textbf{then return} \cr 7 & \qquad\quad ( \mathit{lo} , \mathit{hi} ) \gets \mathrm{pop}( S ); \ \textbf{continue} \cr 8 & \qquad \mathit{mid} \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 9 & \qquad \text{order } A[ \mathit{lo} ] , A[ \mathit{mid} ] , A[ \mathit{hi} ] \text{ by three exchanges} \cr 10 & \qquad A[ \mathit{mid} ] \leftrightarrow A[ \mathit{hi} - 1 ];\ x \gets A[ \mathit{hi} - 1 ]\quad \triangleright\ \text{the median} \cr 11 & \qquad i \gets \mathit{lo};\ j \gets \mathit{hi} - 1\quad \triangleright\ A[ \mathit{lo} ] , A[ \mathit{hi} - 1 ] \text{ stop the scans} \cr 12 & \qquad \textbf{loop} \cr 13 & \qquad\quad \textbf{repeat}\ i \gets i + 1\ \textbf{until}\ x \le A[ i ];\ \textbf{repeat}\ j \gets j - 1\ \textbf{until}\ A[ j ] \le x \cr 14 & \qquad\quad \textbf{if}\ i \ge j\ \textbf{then}\ \textbf{break} \ \textbf{else}\ A[ i ] \leftrightarrow A[ j ] \cr 15 & \qquad A[ i ] \leftrightarrow A[ \mathit{hi} - 1 ] \quad \triangleright\ \text{the pivot in its place } i \cr 16 & \qquad \mathrm{push}( S , \text{the longer of } [ \mathit{lo} , i - 1 ] \text{ and } [ i + 1 , \mathit{hi} ] ) \cr 17 & \qquad ( \mathit{lo} , \mathit{hi} ) \gets \text{the shorter one} \end{array}} \]

where a part that is empty is neither pushed nor sorted.

Postcondition
\( A \) is sorted, with the same items (audit)