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

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

Files

file  ADS/sort/ExchangeSorts.h
 The sorts by exchanges of items, beyond the book, all in place:

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.

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

Function documentation

◆ bubbleSort() [1/3]

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

◆ bubbleSort() [2/3]

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

bubbleSort() above on a vector

◆ bubbleSort() [3/3]

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

In pseudocode (Knuth, section 5.2.2, beyond the book), the items from the last swap on being in their final places:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{bubbleSort}( A , n ) \cr 2 & \quad e \gets n \cr 3 & \quad \textbf{while}\ e > 1\ \textbf{do} \cr 4 & \qquad \mathit{last} \gets 0 \cr 5 & \qquad \textbf{for}\ i \gets 1\ \textbf{to}\ e - 1\ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ A[ i ] < A[ i - 1 ]\ \textbf{then}\ A[ i ] \leftrightarrow A[ i - 1 ];\ \mathit{last} \gets i \cr 7 & \qquad e \gets \mathit{last} \end{array}} \]

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

◆ cocktailShakerSort() [1/3]

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

◆ cocktailShakerSort() [2/3]

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

cocktailShakerSort() above on a vector

◆ cocktailShakerSort() [3/3]

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

In pseudocode (Knuth, section 5.2.2, beyond the book), \( A[ \mathit{lo} .. \mathit{hi} - 1 ] \) the part still to sort:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{cocktailShakerSort}( A , n ) \cr 2 & \quad \mathit{lo} \gets 0;\ \mathit{hi} \gets n \cr 3 & \quad \textbf{while}\ \mathit{hi} - \mathit{lo} > 1\ \textbf{do} \cr 4 & \qquad \text{a pass to the right on } A[ \mathit{lo} .. \mathit{hi} - 1 ];\ \mathit{hi} \gets \text{its last swap} \cr 5 & \qquad \textbf{if}\ \mathit{hi} - \mathit{lo} \le 1\ \textbf{then} \ \textbf{break} \cr 6 & \qquad \text{a pass to the left on } A[ \mathit{lo} .. \mathit{hi} - 1 ];\ \mathit{lo} \gets \text{its last swap} \end{array}} \]

where a pass with no swap sets the bound to the other one.

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

◆ combSort() [1/3]

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

◆ combSort() [2/3]

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

combSort() above on a vector

◆ combSort() [3/3]

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

In pseudocode (Dobosiewicz, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{combSort}( A , n ) \cr 2 & \quad g \gets n;\ \mathit{swapped} \gets \textbf{true} \cr 3 & \quad \textbf{while}\ g > 1\ \textbf{or}\ \mathit{swapped}\ \textbf{do} \cr 4 & \qquad g \gets \max\{ 1 , \lfloor 10 g / 13 \rfloor \}; \ \mathit{swapped} \gets \textbf{false} \cr 5 & \qquad \textbf{for}\ i \gets 0\ \textbf{to}\ n - g - 1\ \textbf{do} \cr 6 & \qquad\quad \textbf{if}\ A[ i + g ] < A[ i ]\ \textbf{then}\ A[ i ] \leftrightarrow A[ i + g ];\ \mathit{swapped} \gets \textbf{true} \end{array}} \]

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

◆ gnomeSort() [1/3]

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

◆ gnomeSort() [2/3]

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

gnomeSort() above on a vector

◆ gnomeSort() [3/3]

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

In pseudocode (Sarbazi-Azad, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{gnomeSort}( A , n ) \cr 2 & \quad i \gets 1 \cr 3 & \quad \textbf{while}\ i < n\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i = 0\ \textbf{or}\ A[ i - 1 ] \le A[ i ] \ \textbf{then}\ i \gets i + 1 \cr 5 & \qquad \textbf{else}\ A[ i ] \leftrightarrow A[ i - 1 ];\ i \gets i - 1 \end{array}} \]

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

◆ oddEvenSort() [1/3]

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

◆ oddEvenSort() [2/3]

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

◆ oddEvenSort() [3/3]

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

In pseudocode (Habermann, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{oddEvenSort}( A , n ) \cr 2 & \quad \textbf{repeat} \cr 3 & \qquad \mathit{sorted} \gets \textbf{true} \cr 4 & \qquad \textbf{foreach}\ s \in ( 0 , 1 )\ \textbf{do} \cr 5 & \qquad\quad \textbf{for}\ i \gets s , s + 2 , \ldots \text{ while } i + 1 < n\ \textbf{do} \cr 6 & \qquad\qquad \textbf{if}\ A[ i + 1 ] < A[ i ]\ \textbf{then}\ A[ i ] \leftrightarrow A[ i + 1 ];\ \mathit{sorted} \gets \textbf{false} \cr 7 & \quad \textbf{until}\ \mathit{sorted} \end{array}} \]

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