# What Is Fibonacci Search in Data Structure With Example?

//

Scott Campbell

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:

• 0
• 1
• 1
• 2
• 3
• 5
• 8
• 13
• ..

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:

1. 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.
2. Initialize two variables, ‘offset’ and ‘prevOffset’, to keep track of the current and previous offsets.
• Note: Initially, set ‘offset’ to 0 and ‘prevOffset’ to 1.
3. While the Target element has not been found and ‘offset’ is less than or equal to the size of the input array, do the following:
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.
2. 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.
3. 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:

1. 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.
2. Initialize ‘offset’ to 0 and ‘prevOffset’ to 1.
3. The Target element (7) is greater than the current element at index ‘offset’ (1). Move one Fibonacci step back by subtracting ‘prevOffset’ from ‘offset’, updating both variables accordingly.
• New values: ‘offset’ = 0, ‘prevOffset’ = -1.
4. The Target element (7) is greater than the current element at index ‘offset’ (1). Move one Fibonacci step back again by subtracting ‘prevOffset’ from ‘offset’, updating both variables accordingly.
• 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.