The Fibonacci search uses the divide and conquer algorithm to sort the array, the Fibonacci search is an addition to the binary search in simple words it is an extension to the binary search.

Given a sorted array arr[] of size n and an element x to be searched in it. Return index of x if it is present in array else return -1.

1 2 3 4 5 6 7 8 9 |
Input: arr[] = {2, 3, 4, 10, 40}, x = 10 Output: 3 Element x is present at index 3. Input: arr[] = {2, 3, 4, 10, 40}, x = 11 Output: -1 Element x is not present. |

Fibonacci Search is a comparison-based technique that uses Fibonacci numbers to search an element in a sorted array.

**Similarities with Binary Search:**

- Works for sorted arrays
- A Divide and Conquer Algorithm.
- Has Log n time complexity.

**Differences with Binary Search**:

- Fibonacci Search divides given array in unequal parts
- Binary Search uses division operator to divide range. Fibonacci Search doesn’t use /, but uses + and -. The division operator may be costly on some CPUs.
- Fibonacci Search examines relatively closer elements in subsequent steps. So when input array is big that cannot fit in CPU cache or even in RAM, Fibonacci Search can be useful.

**Background:**

Fibonacci Numbers are recursively defined as F(n) = F(n-1) + F(n-2), F(0) = 0, F(1) = 1. First few Fibinacci Numbers are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …

**Observations:**

Below observation is used for range elimination, and hence for the O(log(n)) complexity.

1 2 3 4 |
F(n - 2) ≈ (1/3)*F(n) and F(n - 1) ≈ (2/3)*F(n). |

**Algorithm:**

Let the searched element be x.

The idea it to first find the smallest Fibonacci number that is greater than or equal to length of given array. Let the found fibonacci number be fib (m’th fibonacci number). We use (m-2)’th Fibonacci number as index (If it is a valid index). Let (m-2)’th Fibonacci Number be i, we compare arr[i] with x, if x is same, we return i. Else if x is greater, we recur for subarray after i, else we recur for subarray before i.

Below is complete algorithm

Let arr[0..n-1] be th input array and element to be searched be x.

- Find the smallest Fibonacci Number greater than or equal n. Let this number be fibM [m’th Fibonacci Number]. Let the two Fibonacci numbers preceding it be fibMm1 [(m-1)’th Fibonacci Number and fibMm2 [(m-2)’th Fibonacci Number./li>
- While the array has elements to be inspected:
- Compare x with the last element of the range covered by fibMm2
**If**x matches, return index**Else If**x is less than the element, move the three Fibonacci variables two Fibonacci down, indicating elimination of approximately rear two-third of the remaining array.**Else**x is greater than the element, move the three Fibonacci variables one Fibonacci down. Reset offset to index. Together these indicate elimination of approximately front one-third of the remaining array.

- Since there might be a single element remaining for comparison, check if fibMm1 is 1. If Yes, compare x with that remaining element. If match, return index.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//This is a java program to search an element using Fibonacci search import java.util.Scanner; public class Fibonacci_Search { static int kk = -1, nn = -1; static int fib[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 98, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141 }; static int fibsearch(int a[], int n, long x) { int inf = 0, pos, k; if (nn != n) { k = 0; while (fib[k] &lt; n) k++; kk = k; nn = n; } else k = kk; while (k &gt; 0) { pos = inf + fib[--k]; if ((pos &gt;= n) || (x &lt; a[pos])) ; else if (x &gt; a[pos]) { inf = pos + 1; k--; } else return pos; } return -1; } public static void main(String args[]) { int arr[] = { 2, 3, 45, 56, 67, 78, 89, 99, 100, 101 }; int num, pos; Scanner scan = new Scanner(System.in); System.out.println("Enter an element to search: "); num = scan.nextInt(); pos = fibsearch(arr, 10, num); if (pos &gt;= 0) System.out.println("\nElement is at index : " + fibsearch(arr, 10, num)); else System.out.println("\nElement NOT found!! "); scan.close(); } } |

For more articles like this please comment on us.

## Add comment