ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
KnuthSorts.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File KnuthSorts.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
61/*--------------------------------------------------------------------------*/
62/*----------------------------- DEFINITIONS --------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#ifndef __ADS_KnuthSorts
66 #define __ADS_KnuthSorts
67/* self-identification: #endif constant */
68
69/*--------------------------------------------------------------------------*/
70/*------------------------------ INCLUDES ----------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73#include "ADS/Contract.h"
74#include "ADS/sort/RadixSort.h"
75#include "ADS/sort/Sorted.h"
76
77#include <algorithm>
78#include <cmath>
79#include <cstddef>
80#include <functional>
81#include <optional>
82#include <type_traits>
83#include <utility>
84#include <vector>
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------- NAMESPACE ------------------------------------*/
88/*--------------------------------------------------------------------------*/
89
90namespace ADS
91{
92
112template< RankStore Store ,
113 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
114 class C = std::less<> >
115void binaryInsertionSort( Store && A , C less = C() );
116
118template< class T , class C = std::less<> >
119void binaryInsertionSort( std::vector< T > & A , C less = C() );
120
139template< RankStore Store ,
140 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
141 class C = std::less<> >
142void listInsertionSort( Store && A , C less = C() ,
143 std::size_t * moves = nullptr );
144
146template< class T , class C = std::less<> >
147void listInsertionSort( std::vector< T > & A , C less = C() ,
148 std::size_t * moves = nullptr );
149
170template< RankStore Store , class T = std::ranges::range_value_t<
171 std::remove_cvref_t< Store > > >
172void addressCalculationSort( Store && A , std::size_t M = 0 );
173
175template< class T >
176void addressCalculationSort( std::vector< T > & A , std::size_t M = 0 );
177
204template< RankStore Store , RadixKey T = std::ranges::range_value_t<
205 std::remove_cvref_t< Store > > >
206void radixExchangeSort( Store && A );
207
209template< RadixKey T >
210void radixExchangeSort( std::vector< T > & A );
211
235template< RankStore Store ,
236 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
237 class C = std::less<> >
238void tournamentSort( Store && A , C less = C() );
239
241template< class T , class C = std::less<> >
242void tournamentSort( std::vector< T > & A , C less = C() );
243
272template< RankStore Store ,
273 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
274 class C = std::less<> >
275void mergeInsertionSort( Store && A , C less = C() );
276
278template< class T , class C = std::less<> >
279void mergeInsertionSort( std::vector< T > & A , C less = C() );
280
283inline long fordJohnsonBound( std::size_t n );
284
285/*--------------------------------------------------------------------------*/
286/*------------------------- THE SORTS OF A RANGE ---------------------------*/
287/*--------------------------------------------------------------------------*/
288
293template< SortRange R , class... P >
294 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
295 P &&... p )
296{
297 binaryInsertionSort( v , std::forward< P >( p )... );
298 }
299decltype( auto ) binaryInsertionSort( R && A , P &&... p )
300{
301 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
302 return( binaryInsertionSort( v , std::forward< P >( p )... ) );
303 };
304 return( detail::sortRange( A , f , f ) );
305 }
306
311template< SortRange R , class... P >
312 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
313 P &&... p )
314{
315 listInsertionSort( v , std::forward< P >( p )... );
316 }
317decltype( auto ) listInsertionSort( R && A , P &&... p )
318{
319 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
320 return( listInsertionSort( v , std::forward< P >( p )... ) );
321 };
322 return( detail::sortRange( A , f , f ) );
323 }
324
329template< SortRange R , class... P >
330 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
331 P &&... p )
332{
333 addressCalculationSort( v , std::forward< P >( p )... );
334 }
335decltype( auto ) addressCalculationSort( R && A , P &&... p )
336{
337 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
338 return( addressCalculationSort( v , std::forward< P >( p )... ) );
339 };
340 return( detail::sortRange( A , f , f ) );
341 }
342
347template< SortRange R , class... P >
348 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
349 P &&... p )
350{
351 radixExchangeSort( v , std::forward< P >( p )... );
352 }
353decltype( auto ) radixExchangeSort( R && A , P &&... p )
354{
355 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
356 return( radixExchangeSort( v , std::forward< P >( p )... ) );
357 };
358 return( detail::sortRange( A , f , f ) );
359 }
360
365template< SortRange R , class... P >
366 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
367 P &&... p )
368{
369 tournamentSort( v , std::forward< P >( p )... );
370 }
371decltype( auto ) tournamentSort( R && A , P &&... p )
372{
373 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
374 return( tournamentSort( v , std::forward< P >( p )... ) );
375 };
376 return( detail::sortRange( A , f , f ) );
377 }
378
383template< SortRange R , class... P >
384 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
385 P &&... p )
386{
387 mergeInsertionSort( v , std::forward< P >( p )... );
388 }
389decltype( auto ) mergeInsertionSort( R && A , P &&... p )
390{
391 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
392 return( mergeInsertionSort( v , std::forward< P >( p )... ) );
393 };
394 return( detail::sortRange( A , f , f ) );
395 }
396
397/*--------------------------------------------------------------------------*/
398
399} // end( namespace ADS )
400
401/*--------------------------------------------------------------------------*/
402/*-------------------------- IMPLEMENTATION --------------------------------*/
403/*--------------------------------------------------------------------------*/
404
405#include "ADS/sort/impl/KnuthSorts.tpp"
406
407/*--------------------------------------------------------------------------*/
408
409#endif /* KnuthSorts.h included */
410
411/*--------------------------------------------------------------------------*/
412/*------------------------- End File KnuthSorts.h --------------------------*/
413/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort,...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
an integer or a floating point number of at most 8 bytes
Definition RadixSort.h:126
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
void listInsertionSort(Store &&A, C less=C(), std::size_t *moves=nullptr)
the list insertion sort; the moves of the items go to *moves, if given
void mergeInsertionSort(Store &&A, C less=C())
the merge insertion sort of Ford and Johnson
void tournamentSort(Store &&A, C less=C())
the tournament sort
void binaryInsertionSort(Store &&A, C less=C())
the binary insertion sort
void radixExchangeSort(Store &&A)
the radix exchange sort
long fordJohnsonBound(std::size_t n)
the worst case of the comparisons of mergeInsertionSort() on items,
void addressCalculationSort(Store &&A, std::size_t M=0)
the address calculation sort of numbers into lists, if is 0; with an infinite range (an infinity,...
Definition ADT.h:96