|
ADS
Algorithms and Data Structures
|
The search by interpolation of section 12.1 in a sorted vector of numbers: in the part \( A[ i .. j ] \) the key \( v \) is looked for in the place. More...
#include "ADS/Contract.h"#include <algorithm>#include <vector>#include "ADS/search/impl/InterpolationSearch.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| template<class T, class F> | |
| long | ADS::interpolationSearch (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 probe (the ricercaInterpolata() of the book) | |
| template<class T> | |
| long | ADS::interpolationSearch (const std::vector< T > &A, const T &v) |
| the search of interpolationSearch() above, with no trace | |
The search by interpolation of section 12.1 in a sorted vector of numbers: in the part \( A[ i .. j ] \) the key \( v \) is looked for in the place.
\[ m = i + \left\lfloor ( j - i ) \frac{v - A[ i ]}{A[ j ] - A[ i ]} \right\rfloor \]
instead of the middle one, as one opens a phone book near its start to look for a name in C. With keys uniformly distributed it takes \( O( \log \log n ) \) expected time, but \( O( n ) \) in the worst case, so that with few keys or with keys not uniform the binary search is better. The search stops when \( v \) is out of \( [ A[ i ] , A[ j ] ] \), and probes \( A[ i ] \) when \( A[ i ] = A[ j ] \). The fraction is computed in double, and when it is undefined, i.e., \( 0 / 0 \) for distinct keys equal as doubles (integers past \( 2^{53} \)) or \( \infty / \infty \) for infinite keys, the probe is the middle place.
The contracts [see Contract.h]: the vector must be sorted, an audit checked at the level 3, and a miss is checked against a linear scan. The functions are defined in InterpolationSearch.tpp.