fibonacci Algorithm
If the components being searched have non-uniform access memory storage (i. e., the time needed to access a storage location vary depending on the location accessed), the Fibonacci search may have the advantage over binary search in slightly reduce the average time needed to access a storage location.
On average, this leads to about 4 % more comparisons to be executed, but it has the advantage that one only needs addition and subtraction to calculate the index of the accessed array components, while classical binary search needs bit-shift, division or multiplication, operations that were less common at the time Fibonacci search was first published.
#include <iostream>
#include <cassert>
/* Calculate the the value on Fibonacci's sequence given an
integer as input
Fibonacci = 0, 1, 1, 2, 3, 5,
8, 13, 21, 34, 55,
89, 144, ... */
int fibonacci(uint n) {
/* If the input is 0 or 1 just return the same
This will set the first 2 values of the sequence */
if (n <= 1)
return n;
/* Add the last 2 values of the sequence to get next */
return fibonacci(n-1) + fibonacci(n-2);
}
int main() {
int n;
std::cin >> n;
assert(n >= 0);
std::cout << "F(" << n << ")= " << fibonacci(n) << std::endl;
}