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

Insertion Sort (section 2.7.2), as one sorts a hand of cards. More...

Files

file  ADS/sort/InsertionSort.h
 Insertion Sort (section 2.7.2), as one sorts a hand of cards: \( A[ i ] \) is inserted in the sorted prefix \( A[ 0 .. i - 1 ] \), the larger items moving one place to the right.

Functions

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::insertionSort (Store &&A, std::size_t lo, std::size_t hi, C less=C())
 the insertion sort of \( A[ \mathit{lo} .. \mathit{hi} - 1 ] \)
template<class T, class C = std::less<>>
void ADS::insertionSort (std::vector< T > &A, std::size_t lo, std::size_t hi, C less=C())
 insertionSort() 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::insertionSortSentinel (Store &&A, C less=C())
 the insertion sort with a sentinel: the minimum first goes to \( A[ 0 ] \), with \( n - 1 \) comparisons and one swap, so that the inner loop of each insertion needs no test on the index, since it stops at \( A[ 0 ] \) at the latest
template<class T, class C = std::less<>>
void ADS::insertionSortSentinel (std::vector< T > &A, C less=C())
 insertionSortSentinel() 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::insertionSort (Store &&A, C less=C())
 \( A \) in increasing order by less, the insertion sort of the whole vector
template<class T, class C = std::less<>>
void ADS::insertionSort (std::vector< T > &A, C less=C())
 insertionSort() above on a vector
template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { insertionSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::insertionSort (R &&A, P &&... p)
 insertionSort() 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 ) { insertionSortSentinel( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::insertionSortSentinel (R &&A, P &&... p)
 insertionSortSentinel() 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

Insertion Sort (section 2.7.2), as one sorts a hand of cards.

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

Function documentation

◆ insertionSort() [1/5]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { insertionSort( v , std::forward< P >( p )... ); }
decltype(auto) ADS::insertionSort ( R && A,
P &&... p )

insertionSort() 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

◆ insertionSort() [2/5]

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

insertionSort() above on a vector

◆ insertionSort() [3/5]

template<class T, class C = std::less<>>
void ADS::insertionSort ( std::vector< T > & A,
std::size_t lo,
std::size_t hi,
C less = C() )

insertionSort() above on a vector

◆ insertionSort() [4/5]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::insertionSort ( Store && A,
C less = C() )

\( A \) in increasing order by less, the insertion sort of the whole vector

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertionSort}( A ) \cr 2 & \quad \mathrm{insertionSort}( A , 0 , n ) \end{array}} \]

◆ insertionSort() [5/5]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::insertionSort ( Store && A,
std::size_t lo,
std::size_t hi,
C less = C() )

the insertion sort of \( A[ \mathit{lo} .. \mathit{hi} - 1 ] \)

In pseudocode (section 2.7.2), \( A[ \mathit{lo} .. i - 1 ] \) sorted before each insertion:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertionSort}( A , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad \textbf{for}\ i \gets \mathit{lo} + 1\ \textbf{to}\ \mathit{hi} - 1\ \textbf{do}\quad \triangleright\ \text{insert } A[ i ] \cr 3 & \qquad \mathit{temp} \gets A[ i ];\ j \gets i \cr 4 & \qquad \textbf{while}\ j > \mathit{lo}\ \textbf{and}\ \mathit{temp} < A[ j - 1 ]\ \textbf{do} \cr 5 & \qquad\quad A[ j ] \gets A[ j - 1 ];\ j \gets j - 1\quad \triangleright\ \text{a larger item one step right} \cr 6 & \qquad A[ j ] \gets \mathit{temp} \end{array}} \]

Precondition
\( \mathit{lo} \le \mathit{hi} \le n \)
Postcondition
\( A[ \mathit{lo} .. \mathit{hi} - 1 ] \) is sorted, with the same items (audit)

◆ insertionSortSentinel() [1/3]

template<SortRange R, class... P>
requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { insertionSortSentinel( v , std::forward< P >
( p )... ); }
decltype(auto) ADS::insertionSortSentinel ( R && A,
P &&... p )

insertionSortSentinel() 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

◆ insertionSortSentinel() [2/3]

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

insertionSortSentinel() above on a vector

◆ insertionSortSentinel() [3/3]

template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>>
void ADS::insertionSortSentinel ( Store && A,
C less = C() )

the insertion sort with a sentinel: the minimum first goes to \( A[ 0 ] \), with \( n - 1 \) comparisons and one swap, so that the inner loop of each insertion needs no test on the index, since it stops at \( A[ 0 ] \) at the latest

In pseudocode (section 2.7.2, with a sentinel):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insertionSortSentinel}( A , n ) \cr 2 & \quad \textbf{if}\ n < 2\ \textbf{then return} \cr 3 & \quad m \gets \mathrm{argmin}( A , 0 , n - 1 );\ A[ 0 ] \leftrightarrow A[ m ]\quad \triangleright\ \text{the sentinel} \cr 4 & \quad \textbf{for}\ i \gets 2\ \textbf{to}\ n - 1\ \textbf{do} \cr 5 & \qquad x \gets A[ i ];\ j \gets i \cr 6 & \qquad \textbf{while}\ x < A[ j - 1 ]\ \textbf{do}\ A[ j ] \gets A[ j - 1 ];\ j \gets j - 1\quad \triangleright\ \text{no test on } j \cr 7 & \qquad A[ j ] \gets x \end{array}} \]

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