ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ExchangeSorts.h File Reference

The sorts by exchanges of items, beyond the book, all in place: More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/ExchangeSorts.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<>>
void ADS::bubbleSort (Store &&A, C less=C())
 the bubble sort, each pass ending at the last swap of the previous one
template<class T, class C = std::less<>>
void ADS::bubbleSort (std::vector< T > &A, C less=C())
 bubbleSort() 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::cocktailShakerSort (Store &&A, C less=C())
 the bubble sort with passes in both directions
template<class T, class C = std::less<>>
void ADS::cocktailShakerSort (std::vector< T > &A, C less=C())
 cocktailShakerSort() 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::oddEvenSort (Store &&A, C less=C(), std::size_t *rounds=nullptr)
 the odd-even transposition sort; the rounds go to *rounds, if given
template<class T, class C = std::less<>>
void ADS::oddEvenSort (std::vector< T > &A, C less=C(), std::size_t *rounds=nullptr)
 oddEvenSort() 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::combSort (Store &&A, C less=C())
 the comb sort, the gap shrinking by 1.3
template<class T, class C = std::less<>>
void ADS::combSort (std::vector< T > &A, C less=C())
 combSort() 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::gnomeSort (Store &&A, C less=C())
 the gnome sort
template<class T, class C = std::less<>>
void ADS::gnomeSort (std::vector< T > &A, C less=C())
 gnomeSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { bubbleSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::bubbleSort (R &&A, P &&... p)
 bubbleSort() 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 ) { cocktailShakerSort( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::cocktailShakerSort (R &&A, P &&... p)
 cocktailShakerSort() 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 ) { oddEvenSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::oddEvenSort (R &&A, P &&... p)
 oddEvenSort() 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 ) { combSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::combSort (R &&A, P &&... p)
 combSort() 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 ) { gnomeSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::gnomeSort (R &&A, P &&... p)
 gnomeSort() 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

The sorts by exchanges of items, beyond the book, all in place:

  • bubbleSort(), passes from left to right that swap the adjacent items out of order, each ending at the last swap of the previous one, since the items after it are already in their places: \( O( n^2 ) \) comparisons, \( n - 1 \) on a sorted vector, stable;
  • cocktailShakerSort(), the bubble sort with passes alternately to the right and to the left, so that a small item near the end (a turtle) moves to the front in one pass instead of \( n \), stable;
  • oddEvenSort(), the odd-even transposition sort: the pairs \( ( 2 i , 2 i + 1 ) \) and then the pairs \( ( 2 i + 1 , 2 i + 2 ) \) compared and swapped in turns, \( n \) rounds at most, each of independent comparisons, which is why it is a sort for \( n \) processors on a line, stable;
  • combSort(), the bubble sort with a gap that shrinks by 1.3 at each pass, which moves the turtles far, ending as a bubble sort with gap 1: not stable, near \( O( n \log n ) \) in practice, though \( O( n^2 ) \) in the worst case;
  • gnomeSort(), one position that moves right while the two items behind it are in order and swaps them and moves left otherwise: the insertion sort with swaps and no nested loop, \( O( n^2 ) \), stable.

The contracts [see Contract.h]: each sort ends with the same items sorted, an audit. The functions are defined in ExchangeSorts.tpp.

See also
D. E. Knuth, The Art of Computer Programming, vol. 3, 2nd ed., Addison-Wesley, 1998, section 5.2.2 (sorting by exchanging)
A. N. Habermann, Parallel neighbor-sort (or the glory of the induction principle), Technical Report AD-759 248, Carnegie Mellon University, 1972
W. Dobosiewicz, An efficient variation of bubble sort, Information Processing Letters 11(1), 5-6, 1980, doi:10.1016/0020-0190(80)90022-8
H. Sarbazi-Azad, Stupid sort: a new sorting algorithm, Newsletter 599, Department of Computing Science, University of Glasgow, 2000
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa