|
ADS
Algorithms and Data Structures
|
Two searches of a sorted vector from Knuth, beyond the book. More...
Files | |
| file | ADS/search/KnuthSearches.h |
| Two searches of a sorted vector from Knuth, beyond the book [see BinarySearch.h for the binary search of section 1.2]: | |
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.
The definitions, the invariants and the costs of the area are in Theory of searching.
| 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
In pseudocode (Algorithm 6.2.1F of Knuth, beyond the book), the part still to examine being \( A[ o + 1 .. o + F_k - 1 ] \), with \( ( f_2 , f_1 , f ) = ( F_{k-2} , F_{k-1} , F_k ) \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fibonacciSearch}( A , v ) \cr 2 & \quad ( f_2 , f_1 , f ) \gets \text{three consecutive Fibonacci } \text{numbers, the least } f \ge n \cr 3 & \quad o \gets -1 \cr 4 & \quad \textbf{while}\ f > 1\ \textbf{do} \cr 5 & \qquad i \gets \min\{ o + f_2 , n - 1 \} \cr 6 & \qquad \textbf{if}\ A[ i ] < v\ \textbf{then}\ ( f_2 , f_1 , f ) \gets ( f_1 - f_2 , f_2 , f_1 );\ o \gets i \cr 7 & \qquad \textbf{else if}\ v < A[ i ]\ \textbf{then}\ ( f_2 , f_1 , f ) \gets ( 2 f_2 - f_1 , f_1 - f_2 , f_2 ) \cr 8 & \qquad \textbf{else return}\ i \cr 9 & \quad \textbf{if}\ f_1 = 1 \text{ and } o + 1 < n \text{ and } A[ o + 1 ] = v\ \textbf{then return}\ o + 1 \cr 10 & \quad \textbf{return}\ -1 \end{array}} \]
| 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
In pseudocode (Algorithms 6.2.1U and 6.2.1C of Knuth, beyond the book), \( A[ 1 .. n ] \) with \( A[ 0 ] = -\infty \) and \( A[ n + 1 ] = +\infty \), never read:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{uniformBinarySearch}( A , v ) \cr 2 & \quad \textbf{foreach}\ j \ge 1\ \textbf{do}\ \delta_j \gets \lfloor ( n + 2^{j-1} ) / 2^j \rfloor\quad \triangleright\ \text{until } \delta_j = 0 \cr 3 & \quad i \gets \delta_1;\ j \gets 1 \cr 4 & \quad \textbf{loop} \cr 5 & \qquad \textbf{if}\ v < A[ i ]\ \textbf{then} \cr 6 & \qquad\quad j \gets j + 1;\ \textbf{if}\ \delta_j = 0 \ \textbf{then return}\ -1;\ i \gets i - \delta_j \cr 7 & \qquad \textbf{else if}\ v > A[ i ]\ \textbf{then} \cr 8 & \qquad\quad j \gets j + 1;\ \textbf{if}\ \delta_j = 0 \ \textbf{then return}\ -1;\ i \gets i + \delta_j \cr 9 & \qquad \textbf{else return}\ i \end{array}} \]