Exponential search: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Double the search range, then run binary search within it. Input is sorted before searching.

Double a bound until it brackets the target, then binary-search the bounded interval.

Open the 3D visualization

Exponential search example state preview
Preview of the example's final state. Complete values are available in the worked example and interactive timeline.

When to use Exponential search

Searching near the beginning of sorted data.

Choosing an alternative

Binary search uses the whole known array range immediately.

Worked example: input and state changes

Example input

38, 12, 65, 8, 43, 27, 91, 19 | 43

These checkpoints follow the actual example timeline. State details show the stored values and decisions; use the visualization to inspect every intervening step.

  1. Step 0: Initial state.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": []
    }
  2. Step 1: Double range at index 1.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        1
      ]
    }
  3. Step 3: Double range at index 4.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        4
      ]
    }
  4. Step 4: Binary search in range 4–7.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        5
      ]
    }
  5. Step 5: Found 43 at index 5.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        5
      ]
    }

Expected final state

Found 43 at index 5.

Complexity and input bounds

Family: Searching. Time complexity: O(log n).

Space complexity: O(1) auxiliary space.

Working-space bounds describe algorithm state and any explicitly noted output, excluding the visualization's saved timeline. Recording frames can use more memory than running the function alone. Here n is sequence length, V and E are vertex and edge counts, and other parameters refer to the named input dimensions.

2–24 comma-separated integers between 0 and 99. Follow with | target.

The playground validates these limits before starting. Downloaded functions are callable implementations, with parameter and output-buffer contracts in their comments; they do not all repeat the playground's input validation.

Common mistakes and limitations

Clamp a doubled bound before indexing and search the final interval even for a missing target.

Further reading

For background on algorithm analysis and the JavaScript function interface:

Editorial and testing approach · Browse all algorithm guides

Complete JavaScript implementation

javascript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Requires ascending input. Returns a found index, or -1. Reference snippets assume validated playground inputs. Fixed scratch buffers have 256 slots. Sorting variants may use a different traversal strategy than the animation.
function algorithm(a, n, target) {
    if (n == 0) { return -1; }
    let bound = 1;
    while (bound < n && a[bound] < target) { bound *= 2; }
    let l = Math.floor(bound / 2); let r = bound;
    if (r >= n) { r = n - 1; }
    while (l <= r) {
        let mid = l + Math.floor((r - l) / 2);
        if (a[mid] == target) { return mid; }
        if (a[mid] < target) { l = mid + 1; } else { r = mid - 1; }
    }
    return -1;
}

Source

Primary research source for the algorithm or its underlying method. The playground is a bounded teaching implementation, not the publication's original program.