5.1Search Algorithms & Systems

Linear & Binary Search

Compare linear and binary search side by side on the same sorted array. Watch how binary search eliminates half the remaining elements each step, dramatically reducing comparisons compared to linear scanning.

Presets
Array Size20
Linear Search
Time: O(n)
5
0
10
1
15
2
20
3
22
4
25
5
26
6
31
7
34
8
35
9
37
10
38
11
40
12
44
13
46
14
49
15
50
16
52
17
55
18
57
19
Waiting for target...0 comparisons
Binary Search
Time: O(log n)
L
5
0
10
1
15
2
20
3
22
4
25
5
26
6
31
7
34
8
35
9
37
10
38
11
40
12
44
13
46
14
49
15
50
16
52
17
55
18
H
57
19
Waiting for target...0 comparisons
Checking
Checked
Eliminated
Found
Low (L)
Mid (M)
High (H)
1.0x
Linear Steps0
Binary Steps0
Speedup Ratio-x
Array Size20
Theoretical Max20 vs 5
Comparison
PropertyLinear SearchBinary Search
Time (Best)O(1)O(1)
Time (Average)O(n)O(log n)
Time (Worst)O(n)O(log n)
SpaceO(1)O(1)
Requires Sorted?NoYes
ApproachSequential scanDivide & conquer

Linear Search

Scans every element from left to right until the target is found or the end of the array is reached. Simple but inefficient for large arrays. Works on both sorted and unsorted arrays.

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

Binary Search

Repeatedly divides the search space in half by comparing the target with the middle element. Requires a sorted array but achieves logarithmic time complexity, making it vastly superior for large datasets.

mid = (low+high)/2eliminate half each step

Why Does It Matter?

n=100Linear: up to 100 steps. Binary: up to 7 steps.
n=1MLinear: up to 1,000,000 steps. Binary: up to 20 steps.
n=1BLinear: up to 1,000,000,000 steps. Binary: up to 30 steps.
Pseudocode

Linear Search

function linearSearch(arr, target):
for i = 0 to arr.length - 1:
if arr[i] == target:
return i // found
return -1 // not found

Binary Search

function binarySearch(arr, target):
low = 0, high = arr.length - 1
while low <= high:
mid = (low + high) / 2
if arr[mid] == target:
return mid // found
else if arr[mid] < target:
low = mid + 1 // search right
else:
high = mid - 1 // search left
return -1 // not found
Variants & Applications

Binary Search Variants

  • -Lower Bound: Find first occurrence of target or first element greater than target
  • -Upper Bound: Find first element strictly greater than target
  • -Exponential Search: Combine with binary search for unbounded arrays
  • -Interpolation Search: Estimate position using value distribution for O(log log n) average case

Real-World Uses

  • -Database Indexing: B-trees use binary search within nodes for key lookup
  • -Git Bisect: Binary search through commits to find the one that introduced a bug
  • -Dictionary Lookup: Physical dictionaries use alphabetical binary search
  • -IP Routing: Longest prefix matching uses binary search on routing tables

Common Pitfalls

  • -Integer Overflow: Using (low + high) / 2 can overflow; use low + (high - low) / 2 instead
  • -Off-by-One: Incorrect boundary updates (low = mid vs low = mid + 1) cause infinite loops
  • -Unsorted Input: Binary search on unsorted arrays gives incorrect results silently
  • -Floating Point: For continuous domains, use epsilon-based termination condition