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

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. More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/InsertionSort.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::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: \( A[ i ] \) is inserted in the sorted prefix \( A[ 0 .. i - 1 ] \), the larger items moving one place to the right.

\( \Theta( n ) \) time on a sorted vector, \( \Theta( n^2 ) \) on a reversed one and on average (\( n ( n - 1 ) / 4 \) moves, one per inversion), \( O( 1 ) \) space, stable; with its simple inner loop it is the fastest on few items, hence the base case of the hybrid sorts.

The contracts [see Contract.h]: \( \mathit{lo} \le \mathit{hi} \le n \), and the range ends sorted with the same items, an audit. The functions are defined in InsertionSort.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 2.7.2, https://algoritmicamente.it/materiali/libro/capitoli/cap02.pdf
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa