Sunday, March 24, 2024

Fibonacci Search in JavaScript

Must read


Introduction

Fibonacci Search is a type of fascinating algorithms that reveals us the sweetness and class of pc science. Based mostly on the well-known Fibonacci Sequence, the place every quantity is the sum of the 2 previous ones, the Fibonacci Search approach is a comparison-based search algorithm that applies this mathematical idea to search out a component in a sorted array.

Now, you may surprise, how does it work, and much more importantly, and the way can we implement it in JavaScript?! I intention to simply that on this quick article.

Fibonacci Search

The Fibonacci Search makes use of a divide-and-conquer method to go looking a component in a sorted array by leveraging the golden properties of the Fibonacci Sequence. By making a set of Fibonacci numbers that act as indices, the search narrows down the doable areas with assistance from these numbers.

Observe: The Fibonacci Search algorithm is especially environment friendly for giant datasets, because it supplies a median time complexity of O(log n).

Implementing Fibonacci Search in JavaScript

So, let’s get to writing a perform to carry out a Fibonacci Search. Here is what our code may seem like:

perform fibonacciSearch(arr, x) {
    let fibM2 = 0;
    let fibM1 = 1;
    let fibM = fibM2 + fibM1;
    const size = arr.size;

    // Create a Fibonacci sequence better than or equal to the array size
    whereas (fibM < size) {
        fibM2 = fibM1;
        fibM1 = fibM;
        fibM = fibM2 + fibM1;
    }

    let offset = -1;

    whereas (fibM > 1) {
        let i = Math.min(offset + fibM2, size - 1);

        if (arr[i] < x) {
            fibM = fibM1;
            fibM1 = fibM2;
            fibM2 = fibM - fibM1;
            offset = i;
        } else if (arr[i] > x) {
            fibM = fibM2;
            fibM1 = fibM1 - fibM2;
            fibM2 = fibM - fibM1;
        } else {
            return i;
        }
    }

    if (fibM1 && arr[offset + 1] == x) {
        return offset + 1;
    }

    return -1;
}

You should utilize this perform by calling it with the sorted array and the factor you need to discover. It ought to then return the place of the factor in query.

How does it work?

  1. Creating the Fibonacci Sequence: The code initializes the primary two Fibonacci numbers and creates a sequence till it finds a quantity better than or equal to the size of the array.
  2. Dividing the Array: It then divides the array into two components and checks whether or not the factor lies within the first or second half.
  3. Repeating the Course of: The method is then repeated with the brand new subarray, and the method continues till the factor is discovered or the subarray dimension turns into zero.

The Fibonacci Search divides the array primarily based on Fibonacci numbers, which makes the division near the golden ratio.

Conclusion

Fibonacci Search is as an fascinating software of mathematical ideas in pc algorithms. Implementing it in JavaScript provides a recent perspective on dealing with sorted knowledge effectively. This algorithm not solely unveils the fascinating properties of mathematical symmetry in pc science, but in addition offers us a strong answer for looking massive datasets.



Supply hyperlink

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article