Lesson Introduction and Overview

Greetings! Today, we're exploring Binary Search, an efficient algorithm that pinpoints elements in a sorted list. It's like finding a house number on a long street — instead of starting from one end, you begin in the middle and, based on whether the house number is higher or lower, you search the right or left half of the list.

We'll learn to:

  1. Understand Binary Search.
  2. Implement Binary Search using recursion and iteration in JavaScript.
  3. Analyze the time complexity of Binary Search.
Unveiling Binary Search

Binary Search follows a divide-and-conquer strategy. It starts in the middle of a sorted list. If the middle value is the desired one, great! If not, it uses the sorted nature of the list to eliminate half of it. The side to eliminate is selected based on whether the target is smaller or larger than the middle value.

Implementing Binary Search Using Recursion in JavaScript

Let's implement Binary Search in JavaScript using recursion. Here's the code, accompanied by detailed comments:

JavaScript
function recursiveBinarySearch(arr, start, end, target) {
    // Base case: the search area is empty
    if (start > end) return -1;
  
    // Find the midpoint
    let mid = Math.floor((start + end) / 2);
  
    // Found the target
    if (arr[mid] == target) return mid;
  
    // If the target is less than the midpoint, search the left half
    if (arr[mid] > target)
        return recursiveBinarySearch(arr, start, mid - 1, target);
   
    // Else, search the right half
    return recursiveBinarySearch(arr, mid + 1, end, target);
}

This function calls itself recursively, gradually shrinking the search area until it finds the target.

Implementing Binary Search Using Iteration in JavaScript

Here, we create a Binary Search using a while loop in JavaScript:

function iterativeBinarySearch(arr, target) {
    let start = 0;
    let end = arr.length - 1;

    while (start <= end) {
        let mid = Math.floor((start + end) / 2);
        if (arr[mid] == target) return mid; // Found the target
        if (arr[mid] < target) start = mid + 1; // Search the right half
        else end = mid - 1; // Search the left half
    }
    return -1;
}

In this version, the function does not call itself. Instead, it uses a while loop to achieve the same goal.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal