ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
QuickSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File QuickSort.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
53/*--------------------------------------------------------------------------*/
54/*----------------------------- DEFINITIONS --------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#ifndef __ADS_QuickSort
58 #define __ADS_QuickSort
59/* self-identification: #endif constant */
60
61/*--------------------------------------------------------------------------*/
62/*------------------------------ INCLUDES ----------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#include "ADS/Contract.h"
66#include "ADS/sequence/List.h"
68#include "ADS/sort/Sorted.h"
69
70#include <algorithm>
71#include <cstddef>
72#include <functional>
73#include <tuple>
74#include <utility>
75#include <vector>
76
77/*--------------------------------------------------------------------------*/
78/*--------------------------- NAMESPACE ------------------------------------*/
79/*--------------------------------------------------------------------------*/
80
81namespace ADS
82{
83
108template< RankStore Store ,
109 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
110 class C = std::less<> >
111std::size_t partition( Store && A , std::size_t start , std::size_t end ,
112 C less = C() );
113
115template< class T , class C = std::less<> >
116std::size_t partition( std::vector< T > & A , std::size_t start ,
117 std::size_t end , C less = C() );
118
148template< RankStore Store ,
149 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
150 class C = std::less<> >
151std::size_t partitionByValue( Store && A , std::size_t start ,
152 std::size_t end ,
153 const T & x , C less = C() );
154
156template< class T , class C = std::less<> >
157std::size_t partitionByValue( std::vector< T > & A , std::size_t start ,
158 std::size_t end , const T & x , C less = C() );
159
178template< RankStore Store ,
179 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
180 class C = std::less<> >
181void quickSort( Store && A , std::size_t start , std::size_t end ,
182 C less = C() );
183
185template< class T , class C = std::less<> >
186void quickSort( std::vector< T > & A , std::size_t start , std::size_t end ,
187 C less = C() );
188
198template< RankStore Store ,
199 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
200 class C = std::less<> >
201void quickSort( Store && A , C less = C() );
202
204template< class T , class C = std::less<> >
205void quickSort( std::vector< T > & A , C less = C() );
206
249template< RankStore Store ,
250 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
251 class C = std::less<> >
252void quickSortTuned( Store && A , std::size_t cutoff = 16 , C less = C() ,
253 std::size_t * depth = nullptr );
254
256template< class T , class C = std::less<> >
257void quickSortTuned( std::vector< T > & A , std::size_t cutoff = 16 ,
258 C less = C() , std::size_t * depth = nullptr );
259
283template< class T , class C = std::less<> >
284void quickSort( List< T > & L , C less = C() );
285
286/*--------------------------------------------------------------------------*/
287/*------------------------- THE SORTS OF A RANGE ---------------------------*/
288/*--------------------------------------------------------------------------*/
289
294template< SortRange R , class... P >
295 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
296 P &&... p )
297{ quickSort( v , std::forward< P >( p )... ); }
298decltype( auto ) quickSort( R && A , P &&... p )
299{
300 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
301 return( quickSort( v , std::forward< P >( p )... ) );
302 };
303 return( detail::sortRange( A , f , f ) );
304 }
305
310template< SortRange R , class... P >
311 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
312 P &&... p )
313{
314 quickSortTuned( v , std::forward< P >( p )... );
315 }
316decltype( auto ) quickSortTuned( R && A , P &&... p )
317{
318 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
319 return( quickSortTuned( v , std::forward< P >( p )... ) );
320 };
321 return( detail::sortRange( A , f , f ) );
322 }
323
324/*--------------------------------------------------------------------------*/
325
326} // end( namespace ADS )
327
328/*--------------------------------------------------------------------------*/
329/*-------------------------- IMPLEMENTATION --------------------------------*/
330/*--------------------------------------------------------------------------*/
331
332#include "ADS/sort/impl/QuickSort.tpp"
333
334/*--------------------------------------------------------------------------*/
335
336#endif /* QuickSort.h included */
337
338/*--------------------------------------------------------------------------*/
339/*-------------------------- End File QuickSort.h --------------------------*/
340/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Insertion Sort (section 2.7.2), as one sorts a hand of cards: is inserted in the sorted prefix ,...
The LIST of the book: a SEQUENCE [see ADT.h] realized as a bidirectional circular list of cells with ...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
a SEQUENCE realized as a bidirectional circular list with a sentinel, its cells from a pool if Pooled
Definition List.h:118
the storage on which the sorts of the book work in place, which read the items by their ranks only: a...
Definition Sorted.h:184
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
std::optional< Nodes > partition(const std::vector< long > &A)
the places of a half of of half the sum, or nothing
void 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 *de...
std::size_t partitionByValue(Store &&A, std::size_t start, std::size_t end, const T &x, C less=C())
the partition of around a value that need not be in it, in one pass from both ends: the items not l...
void quickSort(Store &&A, std::size_t start, std::size_t end, C less=C())
the sort of , the QuickSort() of the book
Definition ADT.h:96