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

Searching

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. It doesn't require the array to be sorted.

How Linear Search Works

  1. Start at the first element of the array.
  2. Compare the current element with the target value.
  3. If it matches, return its index — you're done.
  4. If it doesn't match, move to the next element.
  5. 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.

Linear SearchO(n)
Check every element left to right until the target is found. Works on unsorted data.
i
4
0
2
1
7
2
1
3
9
4
3
5
Step 1 / 7
Default Current / Compare Swap / Write Settled Active / Path Pivot / Reject Locked (hollow) Target
Linear Search: look for 9, checking left to right.
Python · Searching Algorithms in Code
def linear_search(a, target):
    for i, v in enumerate(a):
        if v == target:
            return i
    return -1

Behind 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

BestO(1) — target is the first element
AverageO(n)
WorstO(n) — target is last or absent
SpaceO(1)

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

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Keep the momentum going

Step through a related structure, pattern or course — the ideas reinforce each other and stick faster.