|
ADS
Algorithms and Data Structures
|
Selection Sort (section 2.7.1). More...
Files | |
| file | ADS/sort/SelectionSort.h |
| Selection Sort (section 2.7.1): the minimum of the part still to sort \( A[ i .. n - 1 ] \) is swapped with \( A[ i ] \), extending the sorted prefix; \( \sum_{i=0}^{n-2} ( n - i - 1 ) = n ( n - 1 ) / 2 \) comparisons whatever the order of the input, \( \Theta( n^2 ) \) time, \( O( 1 ) \) space, not stable. | |
Functions | |
| template<RankStore Store, class T = std::ranges::range_value_t< std::remove_cvref_t< Store > >, class C = std::less<>> | |
| void | ADS::selectionSort (Store &&A, C less=C()) |
| \( A \) in increasing order by less, by selection of the minimum | |
| template<class T, class C = std::less<>> | |
| void | ADS::selectionSort (std::vector< T > &A, C less=C()) |
| selectionSort() above on a vector | |
| template<SortRange R, class... P> requires requires( std::vector< std::ranges::range_value_t< R > > & v , P &&... p ) { selectionSort( v , std::forward< P >( p )... ); } | |
| decltype(auto) | ADS::selectionSort (R &&A, P &&... p) |
| selectionSort() 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 | |
Selection Sort (section 2.7.1).
The definitions, the invariants and the costs of the area are in Theory of sorting.
| decltype(auto) ADS::selectionSort | ( | R && | A, |
| P &&... | p ) |
selectionSort() 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
| void ADS::selectionSort | ( | std::vector< T > & | A, |
| C | less = C() ) |
selectionSort() above on a vector
| void ADS::selectionSort | ( | Store && | A, |
| C | less = C() ) |
\( A \) in increasing order by less, by selection of the minimum
In pseudocode (section 2.7.1), \( A[ 0 .. i - 1 ] \) holding the \( i \) smallest items in order:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{selectionSort}( A , n ) \cr 2 & \quad \textbf{for}\ i \gets 0\ \textbf{to}\ n - 2\ \textbf{do} \cr 3 & \qquad \mathit{pos} \gets \mathrm{argmin}( A , i , n - 1 ) \cr 4 & \qquad A[ i ] \leftrightarrow A[ \mathit{pos} ]\quad \triangleright \ \text{the sorted prefix grows by one} \end{array}} \]