Posted in Information Technology, Software Engineering

The faster way to search the array – Fibonacci search – Coding Security

Overview

 

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

Fibonacci 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.
Examples:

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:

  1. Works for sorted arrays
  2. A Divide and Conquer Algorithm.
  3. Has Log n time complexity.

Differences with Binary Search:

  1. Fibonacci Search divides given array in unequal parts
  2. 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.
  3. 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.

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

Algorithm:
Let the searched element be x.

The idea is to first find the smallest Fibonacci number that is greater than or equal to the length of given array. Let the found Fibonacci number be fib (m’th Fibonacci number). We use (m-2)’th Fibonacci number as the 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 the complete algorithm
Let arr[0..n-1] be the input array and element to be searched be x.

  1. Find the smallest Fibonacci Number greater than or equal to 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].
  2. While the array has elements to be inspected:
    1. Compare x with the last element of the range covered by fibMm2
    2. If x matches, return index
    3. 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.
    4. 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.
  3. 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.

// Java program for Fibonacci Search
import java.util.*;

class Fibonacci
{
// Utility function to find minimum
// of two elements
public static int min(int x, int y)
{ return (x <= y)? x : y; }

/* Returns index of x if present, else returns -1 */
public static int fibMonaccianSearch(int arr[],
int x, int n)
{
/* Initialize fibonacci numbers */
int fibMMm2 = 0; // (m-2)’th Fibonacci No.
int fibMMm1 = 1; // (m-1)’th Fibonacci No.
int fibM = fibMMm2 + fibMMm1; // m’th Fibonacci

/* fibM is going to store the smallest
Fibonacci Number greater than or equal to n */
while (fibM < n)
{
fibMMm2 = fibMMm1;
fibMMm1 = fibM;
fibM = fibMMm2 + fibMMm1;
}

// Marks the eliminated range from front
int offset = -1;

/* while there are elements to be inspected.
Note that we compare arr[fibMm2] with x.
When fibM becomes 1, fibMm2 becomes 0 */
while (fibM > 1)
{
// Check if fibMm2 is a valid location
int i = min(offset+fibMMm2, n-1);

/* If x is greater than the value at
index fibMm2, cut the subarray array
from offset to i */
if (arr[i] < x)
{
fibM = fibMMm1;
fibMMm1 = fibMMm2;
fibMMm2 = fibM – fibMMm1;
offset = i;
}

/* If x is greater than the value at index
fibMm2, cut the subarray after i+1 */
else if (arr[i] > x)
{
fibM = fibMMm2;
fibMMm1 = fibMMm1 – fibMMm2;
fibMMm2 = fibM – fibMMm1;
}

/* element found. return index */
else return i;
}

/* comparing the last element with x */
if(fibMMm1 == 1 && arr[offset+1] == x)
return offset+1;

/*element not found. return -1 */
return -1;
}

// driver code
public static void main(String[] args)
{
int arr[] = {10, 22, 35, 40, 45, 50,
80, 82, 85, 90, 100};
int n = 11;
int x = 85;
System.out.print (“Found at index: “+
fibMonaccianSearch(arr, x, n));
}
}

// This code is contributed by rishabh_jain

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s