What Is Fibonacci Search in Data Structure With Example?
Data structures play a fundamental role in computer science and are essential for efficient data organization and retrieval. One popular search algorithm used in data structures is the Fibonacci search.
The Fibonacci search algorithm is an efficient method for finding a specific element within a sorted array. In this article, we will explore the concept of Fibonacci search and provide an example to illustrate its implementation.
The Fibonacci Sequence
Before diving into the details of the Fibonacci search algorithm, let’s briefly discuss the Fibonacci sequence. The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. The sequence begins as follows:
The key property of the Fibonacci sequence is that each number (after the first two) is equal to the sum of the two preceding numbers. This property forms the basis for the Fibonacci search algorithm.
The Fibonacci Search Algorithm
The Fibonacci search algorithm operates by dividing a sorted array into smaller subarrays using specific indices based on the Fibonacci sequence. It compares the Target element with an element from a particular index and adjusts its position accordingly.
To perform a Fibonacci search, follow these steps:
- Create an array that represents the Fibonacci sequence up to or greater than the size of your input array.
- Note: The size of the Fibonacci sequence array should be chosen such that the last number is greater than or equal to the size of the input array.
- Note: Initially, set ‘offset’ to 0 and ‘prevOffset’ to 1.
- If the Target element is less than the current element at index ‘offset’, move two Fibonacci steps back by setting ‘offset’ to ‘prevOffset’ and update ‘prevOffset’ accordingly.
- If the Target element is greater than the current element at index ‘offset’, move one Fibonacci step back by subtracting ‘prevOffset’ from ‘offset’, updating both variables accordingly.
- If none of the above conditions are met, it means that we have found our Target element. Return its index.
An Example Implementation
Let’s consider an example to illustrate how the Fibonacci search algorithm works. Suppose we have a sorted array:
[1, 3, 5, 7, 9, 11]
We want to find the index of a specific element, let’s say 7. The steps involved in applying Fibonacci search would be as follows:
- Create a Fibonacci sequence array: [0, 1, 1, 2, 3, 5, 8, 13].
- Note: We choose a sequence size greater than or equal to the array size.
- New values: ‘offset’ = 0, ‘prevOffset’ = -1.
- New values: ‘offset’ = -1, ‘prevOffset’ = -2.
. continue these steps until the Target element is found or the end condition is met.
In this example, the algorithm would eventually find the Target element at index 3. The time complexity of Fibonacci search is O(log n), making it an efficient search algorithm for sorted arrays.
In conclusion, the Fibonacci search algorithm provides an efficient method for searching within a sorted array. By leveraging the properties of the Fibonacci sequence, it divides and conquers the search space to locate the desired element. Understanding algorithms like Fibonacci search equips us with powerful tools for data organization and retrieval in various applications.