Linear Search visualization

Check every element from left to right until the target appears.

The simplest search: no ordering required. On average it looks at half the array, and in the worst case every element.

Complexity

Time: best O(1), average O(n), worst O(n). Space: O(1). Difficulty: Beginner.

Pseudocode

procedure linearSearch(A, target):
  for i ← 0 to n−1:
    if A[i] = target: return i
  return −1

More searching algorithms