Step-by-Step Guide to Implementing Binary Search in JavaScript

Binary search is an efficient algorithm for finding a target element in a sorted array. Let’s explore its implementation in JavaScript.

PS4 laying on a table

Photo by Pankaj Patel on Unsplash

Binary search is a powerful algorithm used to efficiently search for a target element in a sorted array. In this blog post, we will explore Binary Search Implementation, where we will dive into the problem description, discuss the solution approach, provide code examples, and explain the implementation details.

Problem Description

The requirement is to implement the binary search algorithm iteratively. Given a sorted array and a target element, our goal is to determine whether the target is present in the array and return its index if found.

Solution Approach

The binary search algorithm operates by repeatedly dividing the search space in half, discarding the half that cannot contain the target element. To implement it iteratively, we initialize two pointers, left and right, to the start and end indices of the array, respectively. We then enter a loop where we calculate the middle index as (left + right) / 2 and compare the element at the middle index with the target. Based on the comparison, we adjust the left and right pointers accordingly until we find the target or determine that it is not present.

Code and Sample Results

function binarySearch(array, target) {
  let left = 0;
  let right = array.length - 1;

  while (left <= right) {
    let mid = Math.floor((left + right) / 2);

    if (array[mid] === target) {
      return mid; // Target found, return the index
    } else if (array[mid] < target) {
      left = mid + 1; // Target is in the right half
    } else {
      right = mid - 1; // Target is in the left half
    }
  }

  return -1; // Target not found
}

const array = [2, 5, 8, 12, 16, 23, 38, 56, 72, 91];
const target = 23;
const result = binarySearch(array, target);

Output

Index of 23 in the array: 5

Code Explanation

We start by defining the binarySearch function, which takes an array and a target as parameters. Inside the function, we initialize the left and right pointers to the start and end indices of the array. We enter a while loop that continues until left becomes greater than right, indicating that the target is not present. Inside the loop, we calculate the middle index using integer division and compare the element at that index with the target. Based on the comparison, we adjust the left and right pointers accordingly. If the target is found, we return the index; otherwise, we return -1 to indicate that the target is not present in the array.

Conclusion

In this blog post, we explored Binary Search Implementation. We discussed the problem description, and the iterative approach to solving it, provided a JavaScript code implementation, and demonstrated its usage with sample results. By understanding and mastering binary search, we can efficiently search for elements in sorted arrays, saving time and resources.

abhinay

© 2024 Abhinay Thakur. All rights are reserved.