DSA Notes
Master linear search completely — sequential search, sentinel search, searching with conditions, finding all occurrences, searching in linked lists and 2D arrays, when to use linear search vs alternatives, and the exact time and space complexity analysis.
What Is Linear Search?
Linear search (also called sequential search) is the simplest search algorithm. It examines every element in a sequence one by one, from start to end, until it finds the target element or exhausts all elements.
No preprocessing is required. The array can be sorted or unsorted, and any data type can be searched as long as equality comparison is possible.
Complete Analysis
Best Case — Ω(1)
Target is the first element:
Worst Case — O(n)
Target is the last element or not present:
Average Case — Θ(n)
Assuming target is equally likely at any position (or not present): Expected comparisons ≈ (n+1)/2 ≈ n/2 = Θ(n)
Variants
Find All Occurrences
Search by Condition (Predicate Search)
Sentinel Linear Search (Optimization)
Why it's faster: The standard loop checks both arr[i] != target AND i < n per iteration. The sentinel eliminates the bounds check by guaranteeing the target will always be found.
Searching in Different Data Structures
Linked List — O(n)
def search_linked_list(head, target):
current = head
index = 0
while current:
if current.data == target:
return index
current = current.next
index += 1
return -12D Array — O(m×n)
When to Use Linear Search
| Scenario | Use Linear Search? |
|---|---|
| Unsorted array, one-time search | ✅ Yes — no preprocessing cost |
| Small array (n < 50) | ✅ Yes — overhead of binary search not worth it |
| Array changes frequently | ✅ Yes — maintaining sorted order costs O(n) |
| Searching by complex condition | ✅ Yes — only option |
| Large sorted array, repeated searches | ❌ No — use binary search O(log n) |
| Fast lookup by key | ❌ No — use hash map O(1) |
Performance Comparison
| Linear search (worst) | 1,000,000 comparisons |
| Binary search | 20 comparisons |
| Hash map lookup | 1 comparison (average) |
| Linear search (worst) | 10 comparisons |
| Binary search | 4 comparisons |
| Hash map lookup | 1 comparison |
Java and C++ Equivalents
Summary
- Definition: Check every element one by one until found or exhausted
- Best: Ω(1) — target at index 0
- Average: Θ(n/2) = Θ(n)
- Worst: O(n) — target absent or at end
- Space: O(1) — no extra memory
- Use when: Array is unsorted, small, or changes frequently
- Optimize with: Sentinel (reduces loop overhead), early termination
*Next Lesson: Binary Search — The O(log n) search algorithm for sorted arrays.*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Linear Search — Complete Guide with All Variants and Applications.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, searching, linear, search
Related Data Structures & Algorithms Topics