Web Analytics

๐Ÿ” Linear Search

Beginner ~10 min read

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

  1. Start at the beginning: Check the first person
  2. Compare: Is this the suspect?
  3. If yes: Found! Stop searching
  4. If no: Move to the next person
  5. 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

Output
Click Run to execute your code
Sentinel Optimization: By placing the target at the end, we can eliminate the end-of-array check in the loop condition. This makes the code slightly faster in practice!

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

  1. Strategy: Check each element sequentially until found
  2. Complexity: O(n) - simple but slow for large data
  3. 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.

Practice: Implement Linear Search from scratch. Try the sentinel optimization. Compare performance with Binary Search on sorted arrays!