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

The minimum of a vector \( A[ 0 .. n - 1 ] \) (section 1.2): min() keeps the minimum so far and compares it with each of the other items, \( n - 1 \) comparisons, which are needed, since each comparison can exclude at most one of the two items from being the minimum (section 2.6). More...

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <ranges>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/search/impl/Minimum.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

Functions

template<class T, class C = std::less<>>
std::size_t ADS::argmin (const std::vector< T > &A, std::size_t start, std::size_t end, C less=C())
 the place of the first minimum of the range \( A[ \mathit{start} .. \mathit{end} ] \)
template<std::ranges::random_access_range R, class C = std::less<>>
requires ( ! std::is_same_v< std::remove_cvref_t< R > , std::vector< std::ranges::range_value_t< R > > > )
std::size_t ADS::argmin (const R &A, std::size_t start, std::size_t end, C less=C())
 argmin() above on any random-access range \( A \) other than a vector (a span, the view of a deque), the ranks of the selection sort on it
template<class T, class C = std::less<>>
const T & ADS::min (const std::vector< T > &A, C less=C())
 the minimum of a vector that is not empty, \( n - 1 \) comparisons
template<class T, class C = std::less<>>
std::pair< T, T > ADS::minMax (const std::vector< T > &A, C less=C())
 the minimum and the maximum together, by pairs: the two items of a pair are compared, the smaller with the minimum and the larger with the maximum, \( \lceil 3 n / 2 \rceil - 2 \) comparisons instead of the \( 2 ( n - 1 ) \) of two searches
template<class T, class C = std::less<>>
std::pair< T, T > ADS::minMaxDivide (const std::vector< T > &A, C less=C())
 the pair of minMax() by divide et impera: the halves solved recursively and their minima and maxima compared, \( T( 2 ) = 1 \), \( T( n ) = 2 T( n / 2 ) + 2 \), i.e., \( 3 n / 2 - 2 \) comparisons for \( n \) a power of 2

Detailed description

The minimum of a vector \( A[ 0 .. n - 1 ] \) (section 1.2): min() keeps the minimum so far and compares it with each of the other items, \( n - 1 \) comparisons, which are needed, since each comparison can exclude at most one of the two items from being the minimum (section 2.6).

argmin() gives the place of the minimum of \( A[ \mathit{start} .. \mathit{end} ] \), the first one if it occurs more than once, as the selection sort of section 2.7.1 asks; minMax() and minMaxDivide(), beyond the book, give the minimum and the maximum together with \( \lceil 3 n / 2 \rceil - 2 \) comparisons, by pairs and by halves, which Pohl proved optimal.

The contracts [see Contract.h]: argmin() needs \( \mathit{start} \le \mathit{end} < n \) and min() a vector that is not empty; that no item of the range is smaller than the one found is an audit, as costly as the search. The functions are defined in Minimum.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 1.2, https://algoritmicamente.it/materiali/libro/capitoli/cap01.pdf, and sections 2.6 and 2.7.1, https://algoritmicamente.it/materiali/libro/capitoli/cap02.pdf
I. Pohl, A sorting problem and its complexity, Communications of the ACM 15(6), 462-464, 1972, doi:10.1145/361405.361423
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa