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

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

#include "ADS/Contract.h"
#include "ADS/search/Minimum.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <utility>
#include <vector>
#include "ADS/sort/impl/SelectionSort.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::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

Detailed description

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.

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

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