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 −1More searching algorithms
- Binary Search — Halve a sorted search range each step by comparing against the middle.
- Jump Search — Jump ahead √n at a time to find the right block, then scan it linearly.
- Interpolation Search — Guess where the target should be from its value, like looking up a name in a phone book.
- Exponential Search — Double a bound until it passes the target, then binary search inside that range.
- Ternary Search — Split a sorted range into thirds with two probes and keep the third that can hold the target.