|
ADS
Algorithms and Data Structures
|
Two searches of a sorted vector from Knuth, beyond the book [see BinarySearch.h for the binary search of section 1.2]: More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <vector>#include "ADS/search/impl/KnuthSearches.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| template<class T> | |
| long | ADS::fibonacciSearch (const std::vector< T > &A, const T &v, long *cmp=nullptr) |
| the Fibonacci search of \( v \) in the sorted \( A \): its place, or -1; the comparisons go to *cmp, if given, the test of the line 9 among them | |
| template<class T> | |
| long | ADS::uniformBinarySearch (const std::vector< T > &A, const T &v, long *cmp=nullptr) |
| the uniform binary search of \( v \) in the sorted \( A \): its place, or -1; the comparisons go to *cmp, if given | |
Two searches of a sorted vector from Knuth, beyond the book [see BinarySearch.h for the binary search of section 1.2]:
Both return the place of \( v \), or -1; when \( n \) is not a Fibonacci number, a probe of the Fibonacci search past the end of the vector is moved to its last item, which is the same as imagining items larger than all the others up to the next Fibonacci number; the uniform search may step to the places 0 and \( n + 1 \), which stand for the keys \( -\infty \) and \( +\infty \) of the book and are compared without reading the vector. The contracts [see Contract.h]: the vector is sorted (an audit), and the result is -1 if and only if \( v \) is not there. The functions are defined in KnuthSearches.tpp.