ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ExchangeSorts.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ExchangeSorts.h --------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_ExchangeSorts
52 #define __ADS_ExchangeSorts
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#include "ADS/Contract.h"
60#include "ADS/sort/Sorted.h"
61
62#include <algorithm>
63#include <cstddef>
64#include <functional>
65#include <utility>
66#include <vector>
67
68/*--------------------------------------------------------------------------*/
69/*--------------------------- NAMESPACE ------------------------------------*/
70/*--------------------------------------------------------------------------*/
71
72namespace ADS
73{
74
92template< RankStore Store ,
93 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
94 class C = std::less<> >
95void bubbleSort( Store && A , C less = C() );
96
98template< class T , class C = std::less<> >
99void bubbleSort( std::vector< T > & A , C less = C() );
100
120template< RankStore Store ,
121 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
122 class C = std::less<> >
123void cocktailShakerSort( Store && A , C less = C() );
124
126template< class T , class C = std::less<> >
127void cocktailShakerSort( std::vector< T > & A , C less = C() );
128
146template< RankStore Store ,
147 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
148 class C = std::less<> >
149void oddEvenSort( Store && A , C less = C() , std::size_t * rounds = nullptr );
150
152template< class T , class C = std::less<> >
153void oddEvenSort( std::vector< T > & A , C less = C() ,
154 std::size_t * rounds = nullptr );
155
173template< RankStore Store ,
174 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
175 class C = std::less<> >
176void combSort( Store && A , C less = C() );
177
179template< class T , class C = std::less<> >
180void combSort( std::vector< T > & A , C less = C() );
181
196template< RankStore Store ,
197 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
198 class C = std::less<> >
199void gnomeSort( Store && A , C less = C() );
200
202template< class T , class C = std::less<> >
203void gnomeSort( std::vector< T > & A , C less = C() );
204
205/*--------------------------------------------------------------------------*/
206/*------------------------- THE SORTS OF A RANGE ---------------------------*/
207/*--------------------------------------------------------------------------*/
208
213template< SortRange R , class... P >
214 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
215 P &&... p )
216{ bubbleSort( v , std::forward< P >( p )... ); }
217decltype( auto ) bubbleSort( R && A , P &&... p )
218{
219 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
220 return( bubbleSort( v , std::forward< P >( p )... ) );
221 };
222 return( detail::sortRange( A , f , f ) );
223 }
224
229template< SortRange R , class... P >
230 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
231 P &&... p )
232{
233 cocktailShakerSort( v , std::forward< P >( p )... );
234 }
235decltype( auto ) cocktailShakerSort( R && A , P &&... p )
236{
237 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
238 return( cocktailShakerSort( v , std::forward< P >( p )... ) );
239 };
240 return( detail::sortRange( A , f , f ) );
241 }
242
247template< SortRange R , class... P >
248 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
249 P &&... p )
250{
251 oddEvenSort( v , std::forward< P >( p )... );
252 }
253decltype( auto ) oddEvenSort( R && A , P &&... p )
254{
255 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
256 return( oddEvenSort( v , std::forward< P >( p )... ) );
257 };
258 return( detail::sortRange( A , f , f ) );
259 }
260
265template< SortRange R , class... P >
266 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
267 P &&... p )
268{ combSort( v , std::forward< P >( p )... ); }
269decltype( auto ) combSort( R && A , P &&... p )
270{
271 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
272 return( combSort( v , std::forward< P >( p )... ) );
273 };
274 return( detail::sortRange( A , f , f ) );
275 }
276
281template< SortRange R , class... P >
282 requires requires( std::vector< std::ranges::range_value_t< R > > & v ,
283 P &&... p )
284{ gnomeSort( v , std::forward< P >( p )... ); }
285decltype( auto ) gnomeSort( R && A , P &&... p )
286{
287 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
288 return( gnomeSort( v , std::forward< P >( p )... ) );
289 };
290 return( detail::sortRange( A , f , f ) );
291 }
292
293/*--------------------------------------------------------------------------*/
294
295} // end( namespace ADS )
296
297/*--------------------------------------------------------------------------*/
298/*-------------------------- IMPLEMENTATION --------------------------------*/
299/*--------------------------------------------------------------------------*/
300
301#include "ADS/sort/impl/ExchangeSorts.tpp"
302
303/*--------------------------------------------------------------------------*/
304
305#endif /* ExchangeSorts.h included */
306
307/*--------------------------------------------------------------------------*/
308/*------------------------ End File ExchangeSorts.h ------------------------*/
309/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The checks of the contracts of the sorts [see Contract.h]: a copy of the input kept only when the aud...
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 bubbleSort(S &s)
the bubble sort: at most passes of swaps of adjacent items out of order, stopped at the first pass w...
void combSort(Store &&A, C less=C())
the comb sort, the gap shrinking by 1.3
void gnomeSort(Store &&A, C less=C())
the gnome sort
void oddEvenSort(Store &&A, C less=C(), std::size_t *rounds=nullptr)
the odd-even transposition sort; the rounds go to *rounds, if given
void cocktailShakerSort(Store &&A, C less=C())
the bubble sort with passes in both directions
Definition ADT.h:96