Algorithms · Visualized
Linear Search
Searching Algorithms
Six search strategies on a sorted array — from the obvious linear scan to log-time binary search and beyond.
Linear Search is the simplest way to find a target value: check every element one by one, from the start of the array to the end, until you find a match or run out of elements. Time: O(n) average, O(n) — target is last or absent worst; space: O(1).
Big-O: Binary O(log n), Linear O(n), Jump O(√n), Interpolation O(log log n) avg, Exponential O(log n).
Linear Search
SearchingLinear Search is the simplest way to find a target value: check every element one by one, from the start of the array to the end, until you find a match or run out of elements. It doesn't require the array to be sorted.
How Linear Search Works
- Start at the first element of the array.
- Compare the current element with the target value.
- If it matches, return its index — you're done.
- If it doesn't match, move to the next element.
- If you reach the end of the array without a match, the target isn't present.
Linear Search Worked Example
Input: {array: [4, 2, 7, 1, 9, 3], target: 9}
Check index0 (4): no match. index1 (2): no. index2 (7): no. index3 (1): no. index4 (9): match! Return index 4.
def linear_search(a, target):
for i, v in enumerate(a):
if v == target:
return i
return -1Behind the Scenes Execution
Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.
Linear Search Time & Space Complexity
When to Use Linear Search
- Unsorted or small datasets, where sorting first wouldn't be worth the cost.
- Linked lists, where you can't jump to the middle so binary search isn't possible.
- As a simple, reliable fallback when data structure guarantees aren't known.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Keep the momentum going
Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.