A logbook for curious waters

Linear search

Inspect items one at a time until the target appears.

Step forward, return to an earlier state, or scrub the timeline. Applying input begins a new run. Switching compatible algorithms retains the input; other algorithms load their own defaults.

Current state

Pseudocode

Each highlighted line corresponds to an operation in the executable trace. Counts describe core algorithm work, excluding validation, snapshots, and rendering.

Read the executable JavaScript

Linear search: the idea

Prerequisites: Arrays, indices, and equality comparisons.

An array stores items at numbered positions called indices. Linear search starts at index zero and compares each value with a target. It returns the first matching index, or reports that no match exists after reaching the end. No ordering is required.

Try it

Choose Last item, then Not present. Both require checking every item. Choose Repeated values to see that the first match is returned.

Reading the display

An inspected cell is pink, a rejected cell is faded, and a found cell is green. Indices start at zero.

Time and memory

Best O(1), worst O(n) value comparisons; O(1) working space. n is the array length.

The bounds above describe core work on prepared input. String conversion to code-point arrays and editor validation are additional work. The visualization saves the data at each operation so you can step backward. This recording requires extra time and memory beyond the algorithm’s own working storage. Input limits keep the demonstrations manageable.

When to use it—and what to watch

Works on unsorted arrays and small collections. Searching repeatedly through a large collection may justify an index or a different data structure.

Continue exploring

Browse all algorithm families

Sources and further reading

Read the algorithm reference