๐ Linear Search
Looking for your keys? You check the table, then the couch, then your pockets, one by one until you find them. That's Linear Search - the simplest and most intuitive searching algorithm. Check each element sequentially until you find what you're looking for!
The Detective's Method
Imagine a detective searching for a suspect in a lineup of people:
The Simple Strategy
- Start at the beginning: Check the first person
- Compare: Is this the suspect?
- If yes: Found! Stop searching
- If no: Move to the next person
- Repeat: Until found or everyone checked
Key insight: Simple, straightforward, works every time!
Why Linear Search?
The Advantages
- Simplest algorithm: Easiest to understand and implement
- Works on any array: Sorted or unsorted - doesn't matter!
- No preprocessing: No need to sort first
- Small arrays: Fast enough for small datasets
- Baseline: Foundation for understanding search algorithms
See It in Action
Watch how Linear Search checks each element sequentially:
Understanding the Process
- Yellow box: Currently checking this element
- Faded boxes: Already checked, not a match
- Green box: Target found!
- Sequential: Checks left to right, one by one
The Code
Click Run to execute your code
Quick Quiz
-
Why is Linear Search O(n) and not O(1)?
Show answer
In the worst case (target is last or not found), we must check ALL n elements. Best case is O(1) if target is first, but worst case determines the complexity: O(n). -
When would you use Linear Search instead of Binary Search?
Show answer
When the array is UNSORTED! Binary Search requires sorted data. Also for very small arrays (< 10 elements), Linear Search is fast enough and simpler. Linear Search is the ONLY option for unsorted data!
Complexity Analysis
| Case | Time | When |
|---|---|---|
| Best | O(1) | Target is first element |
| Average | O(n/2) = O(n) | Target in middle |
| Worst | O(n) | Target is last or not found |
| Space | O(1) | No extra space needed |
Linear vs Binary Search
| Feature | Linear Search | Binary Search |
|---|---|---|
| Time Complexity | O(n) | O(log n) โ |
| Requires Sorted | No โ | Yes |
| Works on Any Array | Yes โ | Sorted only |
| Implementation | Very simple โ | Medium |
| Best For | Small or unsorted | Large and sorted โ |
The Trade-off
Linear Search is slower BUT more flexible:
- Binary Search: 50,000x faster for 1M elements
- But requires sorted data (sorting costs O(n log n))
- Linear Search: Works on ANY array immediately
- For unsorted data: Linear Search is the ONLY option!
When to Use Linear Search
โ Perfect For:
- Unsorted arrays: Only option!
- Small arrays: < 10 elements, fast enough
- Single search: Not worth sorting first
- Linked lists: Can't use Binary Search
- Simplicity matters: Easy to implement and debug
โ Avoid When:
- Large sorted arrays: Use Binary Search instead
- Many searches: Sort once, Binary Search many times
- Performance critical: O(n) too slow for large data
๐ก Key Takeaway
Linear Search is the baseline searching algorithm:
- โ O(n) - checks every element in worst case
- โ Simplest - easiest to understand and implement
- โ Flexible - works on ANY array (sorted or not)
- โ Foundation - basis for understanding search algorithms
- โ ๏ธ Slow for large data - O(n) doesn't scale well
- โ ๏ธ But ONLY option for unsorted data!
Know when to use it vs Binary Search!
Summary
The 3-Point Linear Search Guide
- Strategy: Check each element sequentially until found
- Complexity: O(n) - simple but slow for large data
- Use when: Unsorted data or small arrays
Interview tip: Mention Linear Search as the baseline. Explain it's O(n), works on any array, but Binary Search is O(log n) for sorted data. Show you understand the trade-offs!
What's Next?
You've learned both major search algorithms! Next: Binary Search Variants - solving interview problems with binary search techniques.
Enjoying these tutorials?