See Algorithms
SEE ALGORITHMS

Linear Search Visualization

Linear Search (or sequential search) is the simplest searching algorithm. It inspects each element of an array one by one from start to finish until it finds a match for the target value or reaches the end of the list. Unlike binary or jump-based searches, it requires no prior sorting. Linear Search works seamlessly on arrays, linked lists, and streaming inputs.


Pseudocode

for i = 0 to (n - 1):
    if arr[i] == target: return i
return -1

Visualizer


CheckingChecked (No match)Found

Control speed:


Time & Space Complexity

Metric / Operation
Complexity
Description
Best CaseO(1)When the target element is located at the very first index (index 0).
Average CaseO(n)Assuming uniform hit probability, roughly n / 2 elements are inspected before finding the target.
Worst CaseO(n)When the target sits at the last index or is absent, requiring all n elements to be checked.
Space ComplexityO(1)Inspects elements in place using only a constant loop index.

Common Interview Questions

When is Linear Search preferred over Binary Search?

Linear Search works on unsorted collections, linked lists (where random access is O(n)), and very small arrays where sorting overhead outweighs logarithmic lookup savings.

What is Sentinel Linear Search, and how does it speed up the standard loop?

Sentinel Linear Search temporarily places the target value at the last slot of the array (saving the original element first). Because the target is now guaranteed to be found, the loop no longer needs to check i < n on every iteration, cutting per-step branching in half.

How can Linear Search be optimized when certain keys are queried much more frequently than others?

On unsorted arrays or linked lists, self-organizing heuristics such as Move-to-Front (move each found item to index 0) or Transpose (swap it one step closer to the front) reduce average lookup time on skewed workloads.


💬  Discussion

to join the discussion


Curious to Learn More?

Hand-picked resources to deepen your understanding


Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0