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

The binary search of section 1.2 in a sorted vector \( A[ 0 .. n - 1 ] \). More...

Files

file  ADS/search/BinarySearch.h
 The binary search of section 1.2 in a sorted vector \( A[ 0 .. n - 1 ] \): \( v \) is compared with the middle item \( A[ m ] \) of the part \( A[ i .. j ] \) still to examine, and the search goes on in \( A[ i .. m - 1 ] \) if \( v < A[ m ] \) and in \( A[ m + 1 .. j ] \) if \( v > A[ m ] \); the part halves at each call, so that at most \( \lfloor \log_2 n \rfloor + 1 \) calls are made, 2 comparisons each.

Functions

template<class T, class F>
long ADS::binarySearch (const std::vector< T > &A, const T &v, F &&trace)
 the place of \( v \) in the sorted \( A \), or -1; trace( i , j , m ) on each call that examines \( A[ m ] \)
template<class T>
long ADS::binarySearch (const std::vector< T > &A, const T &v)
 the search of binarySearch() above, with no trace
template<class T>
BinaryTree< T > * ADS::decisionTree (const std::vector< T > &A)
 the decision tree of binarySearch() on \( A \), nil if \( A \) is empty; the caller owns it
template<class T, class C = std::less<>>
std::size_t ADS::lowerBound (const std::vector< T > &A, const T &v, std::size_t lo, std::size_t hi, C less=C())
 the first place in \( [ \mathit{lo} , \mathit{hi} ) \) of an item not smaller than \( v \), \( \mathit{hi} \) if none
template<class T, class C = std::less<>>
std::size_t ADS::lowerBound (const std::vector< T > &A, const T &v, C less=C())
 the place of lowerBound() above in the whole of \( A \), i.e., lowerBound( A , v , 0 , n )
template<class T, class C = std::less<>>
std::size_t ADS::upperBound (const std::vector< T > &A, const T &v, C less=C())
 the first place of an item larger than \( v \), \( n \) if none
template<class T, class C = std::less<>>
std::size_t ADS::gallopSearch (const std::vector< T > &A, const T &v, C less=C())
 the first place \( h \) of an item not smaller than \( v \), found by looking at the places \( 0 , 1 , 3 , 7 , \ldots \) and then by a binary search that leaves out the last place looked at, at most \( 2 \lceil \log_2 ( h + 1 ) \rceil \) comparisons for \( h \ge 1 \) and 1 for \( h = 0 \)

Detailed description

The binary search of section 1.2 in a sorted vector \( A[ 0 .. n - 1 ] \).

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

Function documentation

◆ binarySearch() [1/2]

template<class T>
long ADS::binarySearch ( const std::vector< T > & A,
const T & v )

the search of binarySearch() above, with no trace

◆ binarySearch() [2/2]

template<class T, class F>
long ADS::binarySearch ( const std::vector< T > & A,
const T & v,
F && trace )

the place of \( v \) in the sorted \( A \), or -1; trace( i , j , m ) on each call that examines \( A[ m ] \)

In pseudocode (section 1.2), the tail recursion of the book as a loop:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{binarySearch}( A , v ) \cr 2 & \quad i \gets 0;\ j \gets n - 1\quad \triangleright\ v \text{ can only be in } A[ i .. j ] \cr 3 & \quad \textbf{while}\ i \le j\ \textbf{do} \cr 4 & \qquad m \gets i + \lfloor ( j - i ) / 2 \rfloor;\ \mathrm{trace}( i , j , m ) \cr 5 & \qquad \textbf{if}\ v < A[ m ]\ \textbf{then}\ j \gets m - 1 \cr 6 & \qquad \textbf{else if}\ A[ m ] < v\ \textbf{then}\ i \gets m + 1 \cr 7 & \qquad \textbf{else return}\ m \cr 8 & \quad \textbf{return}\ -1\quad \triangleright\ \text{an empty range} \end{array}} \]

Precondition
\( A \) is sorted
Postcondition
the result is -1 if and only if \( v \) is not in \( A \)

◆ decisionTree()

template<class T>
BinaryTree< T > * ADS::decisionTree ( const std::vector< T > & A)

the decision tree of binarySearch() on \( A \), nil if \( A \) is empty; the caller owns it

In pseudocode (the decision trees of section 2.7.4 on the search of section 1.2), the calls of the search made nodes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{decision}( A , i , j )\quad \triangleright \ \text{the tree of the search in } A[ i .. j ] \cr 2 & \quad \textbf{if}\ j < i\ \textbf{then return nil} \cr 3 & \quad m \gets i + \lfloor ( j - i ) / 2 \rfloor;\ t \gets \text{a new node of item } A[ m ] \cr 4 & \quad t.\mathit{left} \gets \mathrm{decision}( A , i , m - 1 ) \cr 5 & \quad t.\mathit{right} \gets \mathrm{decision}( A , m + 1 , j ) \cr 6 & \quad \textbf{return}\ t \cr 7 & \textbf{function}\ \mathrm{decisionTree}( A ) \cr 8 & \quad \textbf{return}\ \mathrm{decision}( A , 0 , n - 1 ) \end{array}} \]

Precondition
\( A \) is sorted
Postcondition
its invisit is \( A \) (audit)

◆ gallopSearch()

template<class T, class C = std::less<>>
std::size_t ADS::gallopSearch ( const std::vector< T > & A,
const T & v,
C less = C() )

the first place \( h \) of an item not smaller than \( v \), found by looking at the places \( 0 , 1 , 3 , 7 , \ldots \) and then by a binary search that leaves out the last place looked at, at most \( 2 \lceil \log_2 ( h + 1 ) \rceil \) comparisons for \( h \ge 1 \) and 1 for \( h = 0 \)

In pseudocode (exercise 12.9, the unbounded search of Bentley and Yao):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{gallopSearch}( A , v ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} , s ) \gets ( 0 , 0 , 1 )\quad \triangleright\ s \text{ doubles at each probe} \cr 3 & \quad \textbf{while}\ \mathit{hi} < n\ \textbf{and}\ A[ \mathit{hi} ] < v\ \textbf{do} \cr 4 & \qquad ( \mathit{lo} , \mathit{hi} , s ) \gets ( \mathit{hi} + 1 , \mathit{hi} + s , 2 s ) \cr 5 & \quad \textbf{return}\ \mathrm{lowerBound}( A , v , \mathit{lo} , \min\{ \mathit{hi} , n \} )\quad \triangleright\ v \le A[ \mathit{hi} ] \text{ if } \mathit{hi} < n \end{array}} \]

Precondition
\( A \) is sorted
Postcondition
\( A[ r - 1 ] < v \le A[ r ] \)

◆ lowerBound() [1/2]

template<class T, class C = std::less<>>
std::size_t ADS::lowerBound ( const std::vector< T > & A,
const T & v,
C less = C() )

the place of lowerBound() above in the whole of \( A \), i.e., lowerBound( A , v , 0 , n )

◆ lowerBound() [2/2]

template<class T, class C = std::less<>>
std::size_t ADS::lowerBound ( const std::vector< T > & A,
const T & v,
std::size_t lo,
std::size_t hi,
C less = C() )

the first place in \( [ \mathit{lo} , \mathit{hi} ) \) of an item not smaller than \( v \), \( \mathit{hi} \) if none

In pseudocode (exercise 12.13), the invariant being \( A[ \mathit{lo} - 1 ] < v \le A[ \mathit{hi} ] \) (outside the range, \( -\infty \) and \( +\infty \)):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{lowerBound}( A , v , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do}\quad \triangleright\ A[ .. \mathit{lo} - 1 ] < v \le A[ \mathit{hi} .. ] \cr 3 & \qquad m \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 4 & \qquad \textbf{if}\ v \le A[ m ]\ \textbf{then}\ \mathit{hi} \gets m \ \textbf{else}\ \mathit{lo} \gets m + 1 \cr 5 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]

Precondition
\( \mathit{lo} \le \mathit{hi} \le n \) and \( A[ \mathit{lo} .. \mathit{hi} ) \) is sorted
Postcondition
\( A[ r - 1 ] < v \le A[ r ] \), within \( [ \mathit{lo} , \mathit{hi} ] \)

◆ upperBound()

template<class T, class C = std::less<>>
std::size_t ADS::upperBound ( const std::vector< T > & A,
const T & v,
C less = C() )

the first place of an item larger than \( v \), \( n \) if none

In pseudocode (exercise 12.13), the search of lowerBound() with the test reversed:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{upperBound}( A , v ) \cr 2 & \quad ( \mathit{lo} , \mathit{hi} ) \gets ( 0 , n )\quad \triangleright\ A[ .. \mathit{lo} - 1 ] \le v < A[ \mathit{hi} .. ] \cr 3 & \quad \textbf{while}\ \mathit{lo} < \mathit{hi}\ \textbf{do} \cr 4 & \qquad m \gets \mathit{lo} + \lfloor ( \mathit{hi} - \mathit{lo} ) / 2 \rfloor \cr 5 & \qquad \textbf{if}\ A[ m ] \le v\ \textbf{then}\ \mathit{lo} \gets m + 1\ \textbf{else}\ \mathit{hi} \gets m \cr 6 & \quad \textbf{return}\ \mathit{lo} \end{array}} \]

Precondition
\( A \) is sorted
Postcondition
\( A[ r - 1 ] \le v < A[ r ] \)