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
- Binary search — Halve a sorted search interval after each comparison.