🔪 Binary Search

How to implement binary search in JavaScript

What is the time complexity of binary search?

Binary search is a faster way to find an item in a sorted array with O(log n) time complexity, compared to a regular loop with O(n) time complexity.

Binary Search Interview Question

Create a function that takes a sorted array and a target value. Return the index of the target value in the array. If the target value is not in the array, return -1.

Binary Search Implementation

function search(arr, target, start=0, end=arr.length-1) {

    console.log(start, end)

    if (start > end) {
        console.log('Not found!');
        return -1;
    } 

    const middle = Math.floor( (start + end) / 2 );

    if (arr[middle] === target) {
        console.log(`${target} Found at index ${middle}`);
        return middle;
    } 

    if(arr[middle] > target) {
        return search(arr, target, start, middle-1);
    }

    if(arr[middle] < target) {
        return search(arr, target, middle+1, end);
    }

}

const arr = ['a', 'b', 'c', 'x', 'y', 'z'];
console.log(search(arr, 'b'));

Questions? Let's chat

Open Discord