Linear search: Examples & Code

Algorithm Visualizations November 16, 2026 javascript

Published by Dimitris Kougioumtzis · Updated

Inspect values sequentially.

Inspect positions in sequence until a value matches or the list ends; no ordering assumption is needed.

Open the 3D visualization

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

Unsorted data and one-off short-list queries.

Choosing an alternative

Binary search is faster on pre-sorted data, but sorting first has its own cost.

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": [
        38,
        12,
        65,
        8,
        43,
        27,
        91,
        19
      ],
      "active": []
    }
  2. Step 2: Compare 12 with 43.

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

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

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

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

Expected final state

Found 43 at index 4.

Complexity and input bounds

Family: Searching. Time complexity: O(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

Index zero is a valid match, not a false Boolean result.

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
// Returns the 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) {
    for (let i = 0; i < n; i++) { if (a[i] == target) { return i; } }
    return -1;
}

Source

No original paper has been verified for this entry. This source is a reference text or teaching reference, not an asserted original publication.