JavaScript Program to Implement Exponential Search

JavaScript Program to Implement Exponential Search

Exponential Search is an efficient algorithm designed for searching elements in a sorted array. Unlike linear search, which checks each element one by one, Exponential Search quickly finds a range where the target element might exist and then uses a simpler search like Binary Search to locate it. The key idea is to increase the index exponentially, which makes it much faster than linear search for large datasets.

Pluralsight Logo
Accelerate your tech career
with hands-on learning.
Whether you're a tech newbie or a total pro,
get the skills and confidence to land your next move.
Start 10-Day Free Trial

For beginners, Exponential Search is a great way to understand how combining two techniques—exponential index growth and Binary Search—can improve performance. It is particularly useful in scenarios where the element to be searched is closer to the beginning of a large sorted array. Examples include searching in databases, sorted lists of numbers, or even in search engines where sorted data structures are common. Learning this algorithm builds a strong foundation in efficient search strategies and helps beginners get comfortable with combining algorithms in practical programming.

Program 1: Basic Exponential Search

This program demonstrates a simple Exponential Search in a sorted array.

function binarySearch(arr, left, right, target) {

    while (left <= right) {

        let mid = left + Math.floor((right - left) / 2);
        if (arr[mid] === target) return mid;
        if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;

    }

    return -1;

}

function exponentialSearch(arr, target) {

    if (arr[0] === target) return 0;
    let i = 1;

    while (i < arr.length && arr[i] <= target) {
        i *= 2;
    }

    return binarySearch(arr, i / 2, Math.min(i, arr.length - 1), target);

}

let numbers = [1, 3, 5, 7, 9, 11, 13, 15];
let target = 9;
let result = exponentialSearch(numbers, target);

console.log(result !== -1 ? `Element found at index ${result}` : "Element not found");

This program works by first finding a range where the target element could be, then performing a Binary Search in that range. Beginners can understand it as “jump ahead exponentially until you overshoot, then search carefully in the smaller section.” It is useful because it reduces unnecessary comparisons, making it faster for large arrays compared to Linear Search.

Program 2: Recursive Exponential Search

This example shows Exponential Search implemented with recursion for the Binary Search part.

function binarySearchRecursive(arr, left, right, target) {

    if (left > right) return -1;

    let mid = left + Math.floor((right - left) / 2);

    if (arr[mid] === target) return mid;
    if (arr[mid] < target) return binarySearchRecursive(arr, mid + 1, right, target);

    return binarySearchRecursive(arr, left, mid - 1, target);

}

function exponentialSearchRecursive(arr, target) {

    if (arr[0] === target) return 0;
    let i = 1;
    while (i < arr.length && arr[i] <= target) i *= 2;

    return binarySearchRecursive(arr, i / 2, Math.min(i, arr.length - 1), target);

}

let values = [2, 4, 6, 8, 10, 12, 14, 16];
let searchTarget = 10;
let resultRecursive = exponentialSearchRecursive(values, searchTarget);

console.log(resultRecursive !== -1 ? `Element found at index ${resultRecursive}` : "Element not found");

This recursive approach shows how the search can be split into a combination of exponential growth and recursive binary searching. Beginners can understand recursion as “divide the search space and repeat the same process on smaller parts.” It is useful for understanding how algorithms can be combined for efficiency.

Program 3: Exponential Search for first occurrence

This program demonstrates how to find the first occurrence of a target in a sorted array with duplicates.

function binarySearchFirst(arr, left, right, target) {

    let result = -1;

    while (left <= right) {

        let mid = left + Math.floor((right - left) / 2);

        if (arr[mid] === target) {
            result = mid;
            right = mid - 1;
        } else if (arr[mid] < target) left = mid + 1;

        else right = mid - 1;

    }

    return result;

}

function exponentialSearchFirst(arr, target) {

    if (arr[0] === target) return 0;

    let i = 1;

    while (i < arr.length && arr[i] <= target) i *= 2;

    return binarySearchFirst(arr, i / 2, Math.min(i, arr.length - 1), target);

}

let numbersDup = [1, 2, 2, 2, 3, 4, 5];
let targetDup = 2;

console.log("First occurrence index:", exponentialSearchFirst(numbersDup, targetDup));

This program works by first locating the range exponentially and then using a modified binary search to find the first match. Beginners can understand it as “find the block quickly and then search carefully for the first match.” It is useful for datasets containing duplicates.

Program 4: Exponential Search for last occurrence

This version locates the last occurrence of a target in a sorted array.

function binarySearchLast(arr, left, right, target) {

    let result = -1;

    while (left <= right) {

        let mid = left + Math.floor((right - left) / 2);

        if (arr[mid] === target) {
            result = mid;
            left = mid + 1;
        } else if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;

    }

    return result;

}

function exponentialSearchLast(arr, target) {

    if (arr[0] === target) return 0;

    let i = 1;

    while (i < arr.length && arr[i] <= target) i *= 2;

    return binarySearchLast(arr, i / 2, Math.min(i, arr.length - 1), target);

}

let data = [5, 7, 7, 7, 8, 9, 10];
let targetValue = 7;

console.log("Last occurrence index:", exponentialSearchLast(data, targetValue));

This program uses a similar exponential range detection but adjusts binary search to find the last match. Beginners can think of it as “search right in the block to find the last occurrence.” It is useful when working with sorted arrays containing repeated elements.

Program 5: Exponential Search as a reusable function

This example wraps Exponential Search into a reusable function suitable for multiple datasets.

function exponentialSearchUtility(arr, target) {

    if (arr[0] === target) return 0;

    let i = 1;

    while (i < arr.length && arr[i] <= target) i *= 2;

    let left = i / 2;
    let right = Math.min(i, arr.length - 1);

    while (left <= right) {

        let mid = left + Math.floor((right - left) / 2);

        if (arr[mid] === target) return mid;
        else if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;

    }

    return -1;

}

let numbersSet = [3, 6, 9, 12, 15, 18, 21];
let findNum = 12;
let resultUtility = exponentialSearchUtility(numbersSet, findNum);

console.log(resultUtility !== -1 ? `Element found at index ${resultUtility}` : "Element not found");

This reusable function makes it easy for beginners to apply Exponential Search across different sorted arrays, demonstrating modularity and efficient searching in practice.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Exponential Search in JavaScript.

Q1. What is Exponential Search?
Exponential Search is a search algorithm that finds a range for the target element by exponentially increasing the index, then performs Binary Search in that range.

Q2. Can Exponential Search be used on unsorted arrays?
No, Exponential Search requires a sorted array to function correctly.

Q3. How is Exponential Search different from Binary Search?
Binary Search divides the array in half each time, while Exponential Search quickly finds a probable range and then uses Binary Search, which can be faster for elements near the beginning.

Q4. What is the time complexity of Exponential Search?
The time complexity is O(log n) for the Binary Search step, and finding the range takes O(log i), where i is the position of the target, giving efficient performance.

Q5. Why should beginners learn Exponential Search?
It introduces range detection and combines two techniques, teaching efficiency, modular programming, and how to handle large datasets.

Conclusion

Exponential Search is a powerful searching algorithm for sorted arrays that combines exponential range detection with Binary Search.

By practicing iterative, recursive, first/last occurrence, and reusable function implementations, beginners can strengthen their understanding of efficient search strategies in JavaScript. Learning Exponential Search helps build confidence in handling large datasets, optimizing algorithms, and combining multiple techniques to solve practical programming challenges.

Scroll to Top