Binary Search Using JavaScript

  1. Binary Search Through Iterative Method in JavaScript
  2. Binary Search Through Recursive Method in JavaScript
Binary Search Using JavaScript

This article will explore using two methods to binary search an element using JavaScript.

The first is an iterative way, and the second is a recursive way.

Binary Search Through Iterative Method in JavaScript

let binarySearchIterativeMethod =
    function(myValuesArray, searchingElement) {
  let startingIndex = 0, endingIndex = myValuesArray.length - 1;

  while (startingIndex <= endingIndex) {
    let midIndex = Math.floor((startingIndex + endingIndex) / 2);
    if (myValuesArray[midIndex] === searchingElement)
      return true;
    else if (myValuesArray[midIndex] < searchingElement)
      startingIndex = midIndex + 1;
    else
      endingIndex = midIndex - 1;
  }

  return false;
}

let myValuesArray = [1, 3, 5, 7, 8, 9];
let searchingElement = 5;

if (binarySearchIterativeMethod(myValuesArray, searchingElement))
  console.log('Element found in an Array');
else
  console.log('Element not found an Array');
searchingElement = 6;

if (binarySearchIterativeMethod(myValuesArray, searchingElement))
  console.log('Element found in an Array');
else
  console.log('Element not found in an Array');

Output:

For searchingElement value 5 we got
      Element found in an Array
For searchingElement value 6 because its not in our array we got
      Element not found in an Array

In the above code, we first declare a function named binarySearchIterativeMethod, which takes two parameters. The first is the array, and the second is the element we want to search.

This function declares and initializes two variables like startingIndex and endingIndex. The startingIndex is initialized with zero because we want to iterate our array from the first index.

The ending index is initialized with the length of the array. After that, we will make a while loop that will iterate the whole array until the condition is satisfied.

This loop will iterate the array till the startingIndex is less than or equal to endingIndex. Simply it means that there is no illegal index of an array that will test.

If, in any case, there is an illegal index that occurs, it will terminate the loop. In this loop, firstly, we are getting the middleIndex of the array and taking its floor value.

It means that if startingIndex is 0 and endingIndex is 9. If we divide this value by 2, we will get 4.5, which is not a valid index. So we are taking the floor value of it like 5.

Then we will check if searchingElement is present on the middleIndex's value then return true. If it is not happening, we will check if our searchingElement is lesser than the middleIndex value then we perform searches on the left side.

If our searchingElement is greater than the middleIndex of the given array, we are searching from the right side of the array. If these cases do not happen, then we return false.

After that, we will make an array, initialize it, and then make a variable named searchingElement and initialize it with the value 6.

After it, we call the function binarySearchIterativeMethod and pass the array and searching element and checking it if this function returns true we will output value is found. If a function is not returning true, we show Item is not found.

Binary Search Through Recursive Method in JavaScript

let binarySearchRecursiveFunction =
    function(arr, x, startIndex, endIndex) {
  if (startIndex > endIndex) return false;
  let middleIndex = Math.floor((startIndex + endIndex) / 2);

  if (arr[middleIndex] === x) return true;

  if (arr[middleIndex] > x)
    return binarySearchRecursiveFunction(arr, x, startIndex, middleIndex - 1);
  else
    return binarySearchRecursiveFunction(arr, x, middleIndex + 1, endIndex);
}

let arr = [1, 3, 5, 7, 8, 9];
let x = 5;

if (binarySearchRecursiveFunction(arr, x, 0, arr.length - 1))
  console.log('Element found in an Array');
else
  console.log('Element not found in an Array');

x = 9;

if (binarySearchRecursiveFunction(arr, x, 0, arr.length - 1))
  console.log('Element found in an Array');
else
  console.log('Element not found in an Array');

Output:

For searchingElement value 5 we got
      Element found in an Array
For searchingElement value 9 because its not in our array we got
      Element not found in an Array

In the above function, we first check that if startIndex is greater than endIndex, we will return false.

Then, we will check if the searching element named x is equal to the middle index of giving array, then we will return true. If the searching element is lesser than the middle index, we search from the left side of an array.

If the searching element is greater than the middle element of array, we will search from the binary tree’s right side.