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

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

#include "ADS/Contract.h"
#include "ADS/sort/Sorted.h"
#include "ADS/tree/BinaryTree.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <vector>
#include "ADS/search/impl/BinarySearch.tpp"

Go to the source code of this file.

Namespaces

namespace  ADS

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 ] \): \( 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.

The middle is computed as \( m = i + \lfloor ( j - i ) / 2 \rfloor \), which unlike \( \lfloor ( i + j ) / 2 \rfloor \) cannot overflow (the bug of Arrays.binarySearch of Java until 2003). The search may report each call \( ( i , j , m ) \) to a function, as the table of the example.

decisionTree() gives the decision tree of the search, the binary tree whose root is the middle item \( A[ m ] \) of the vector and whose subtrees are the decision trees of \( A[ 0 .. m - 1 ] \) and of \( A[ m + 1 .. n - 1 ] \): a search for \( v \) is the path from the root down to the node of \( v \), or to a missing child, so that its comparisons are the depth of that node plus one. Its invisit gives \( A \), it is a binary search tree [see SearchTree.h] and all its levels are full but the last one; hence its height is \( \lfloor \log_2 n \rfloor \), the worst case of the search.

lowerBound() and upperBound() give the first place of an item not smaller, and larger, than \( v \), with \( \lceil \log_2 ( n + 1 ) \rceil \) comparisons: their difference is the number of occurrences of \( v \) (exercise 12.13). gallopSearch() looks for the first place of an item not smaller than \( v \) by doubling a bound before the binary search, in \( O( \log h ) \) time if that place is \( h \), at most \( 2 \lceil \log_2 ( h + 1 ) \rceil \) comparisons for \( h \ge 1 \) (exercise 12.9).

The contracts [see Contract.h]: the vector must be sorted, an audit checked at the level 3 since it costs \( O( n ) \) against the \( O( \log n ) \) of the search; lowerBound() also needs \( \mathit{lo} \le \mathit{hi} \le n \), and the place it returns is checked against its two neighbors. The functions are defined in BinarySearch.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 1.2, https://algoritmicamente.it/materiali/libro/capitoli/cap01.pdf
J. L. Bentley, A. C.-C. Yao, An almost optimal algorithm for unbounded searching, Information Processing Letters 5(3), 82-87, 1976, doi:10.1016/0020-0190(76)90071-5
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa