Fibonacci search: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Probe a sorted array using Fibonacci-sized intervals. Input is sorted first.

Use neighboring Fibonacci numbers to partition the search interval and reduce those numbers after each probe.

Open the 3D visualization

Fibonacci 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 Fibonacci search

Comparing addition-based interval updates with midpoint search.

Choosing an alternative

Binary search is the usual simple random-access default.

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: Probe index 2; Fibonacci interval 8.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        2
      ]
    }
  3. Step 2: Probe index 4; Fibonacci interval 5.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        4
      ]
    }
  4. Step 3: Probe index 5; Fibonacci interval 3.

    State at this checkpoint
    {
      "values": [
        8,
        12,
        19,
        27,
        38,
        43,
        65,
        91
      ],
      "active": [
        5
      ]
    }
  5. Step 4: 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

Offsets and the final candidate require explicit bounds checks near the array end.

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
// 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) {
    let f2 = 0; let f1 = 1; let f = 1; let offset = -1;
    while (f < n) { f2 = f1; f1 = f; f = f1 + f2; }
    while (f > 1) {
        let i = offset + f2; if (i >= n) { i = n - 1; }
        if (a[i] < target) { f = f1; f1 = f2; f2 = f - f1; offset = i; }
        else if (a[i] > target) { f = f2; f1 = f1 - f2; f2 = f - f1; }
        else { return i; }
    }
    if (f1 > 0 && offset + 1 < n && a[offset + 1] == target) { return offset + 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.