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

The minimum of a vector \( A[ 0 .. n - 1 ] \) (section 1.2). More...

Files

file  ADS/search/Minimum.h
 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).

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

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

Function documentation

◆ argmin() [1/2]

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

Precondition
\( \mathit{start} \le \mathit{end} < n \)

◆ argmin() [2/2]

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} ] \)

In pseudocode (section 2.7.1, where the selection sort calls it):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{argmin}( A , \mathit{start} , \mathit{end} ) \cr 2 & \quad \mathit{pos} \gets \mathit{start}\quad \triangleright\ \text{the first minimum so far} \cr 3 & \quad \textbf{for}\ j \gets \mathit{start} + 1\ \textbf{to} \ \mathit{end}\ \textbf{do}\ \textbf{if}\ A[ j ] < A[ \mathit{pos} ] \ \textbf{then}\ \mathit{pos} \gets j \cr 4 & \quad \textbf{return}\ \mathit{pos} \end{array}} \]

Precondition
\( \mathit{start} \le \mathit{end} < n \)
Postcondition
no item of \( A[ \mathit{start} .. \mathit{end} ] \) is smaller than \( A[ \mathit{pos} ] \) (audit)

◆ min()

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

In pseudocode (section 1.2):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{min}( A ) \cr 2 & \quad \textbf{return}\ A[ \mathrm{argmin}( A , 0 , n - 1 ) ] \end{array}} \]

Precondition
\( A \) is not empty

◆ minMax()

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

In pseudocode (Pohl, beyond the book), 3 comparisons for each pair:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minMax}( A ) \cr 2 & \quad \mathit{lo} \gets A[ 0 ];\ \mathit{hi} \gets A[ 0 ];\ i \gets 1 \cr 3 & \quad \textbf{if}\ n \text{ is even}\ \textbf{then} \cr 4 & \qquad \textbf{if}\ A[ 1 ] < A[ 0 ]\ \textbf{then}\ \mathit{lo} \gets A[ 1 ]\ \textbf{else}\ \mathit{hi} \gets A[ 1 ] \cr 5 & \qquad i \gets 2 \cr 6 & \quad \textbf{while}\ i + 1 < n\ \textbf{do} \cr 7 & \qquad ( a , b ) \gets A[ i ] , A[ i + 1 ] \text{, the smaller first} \cr 8 & \qquad \textbf{if}\ a < \mathit{lo}\ \textbf{then}\ \mathit{lo} \gets a \cr 9 & \qquad \textbf{if}\ \mathit{hi} < b\ \textbf{then}\ \mathit{hi} \gets b \cr 10 & \qquad i \gets i + 2 \cr 11 & \quad \textbf{return}\ ( \mathit{lo} , \mathit{hi} ) \end{array}} \]

Precondition
\( A \) is not empty
Postcondition
the minimum and the maximum of \( A \) (audit)

◆ minMaxDivide()

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

In pseudocode (Pohl, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{minMaxRec}( A , i , j ) \cr 2 & \quad \textbf{if}\ i = j\ \textbf{then return}\ ( A[ i ] , A[ i ] ) \cr 3 & \quad \textbf{if}\ j = i + 1\ \textbf{then return}\ ( A[ i ] , A[ j ] ) \text{, the smaller first} \cr 4 & \quad m \gets i + \lfloor ( j - i ) / 2 \rfloor \cr 5 & \quad ( a , b ) \gets \mathrm{minMaxRec}( A , i , m );\ ( c , d ) \gets \mathrm{minMaxRec}( A , m + 1 , j ) \cr 6 & \quad \textbf{return}\ ( \min\{ a , c \} , \max\{ b , d \} ) \cr 7 & \textbf{function}\ \mathrm{minMaxDivide}( A ) \cr 8 & \quad \textbf{return}\ \mathrm{minMaxRec}( A , 0 , n - 1 ) \end{array}} \]

Precondition
\( A \) is not empty
Postcondition
the minimum and the maximum of \( A \) (audit)