DSA Notes
A comprehensive comparison of all searching algorithms — linear search, binary search, interpolation search, ternary search, hash-based search, and tree-based search — with performance tables, use cases, and a decision guide for every scenario.
All Search Algorithms at a Glance
| Algorithm | Time (Average) | Time (Worst) | Space | Requires | Best For |
|---|---|---|---|---|---|
| Linear Search | O(n) | O(n) | O(1) | Nothing | Unsorted, small arrays |
| Binary Search | O(log n) | O(log n) | O(1) | Sorted array | Sorted arrays, repeated queries |
| Interpolation | O(log log n) | O(n) | O(1) | Sorted + Uniform | Uniformly distributed sorted data |
| Ternary Search | O(log n) | O(log n) | O(1) | Unimodal | Finding max/min of unimodal function |
| Hash Map Lookup | O(1) average | O(n) | O(n) | Hash map built | Exact match, repeated queries |
| BST Search | O(log n) | O(n) | O(n) | Balanced BST | Dynamic sorted data |
| Trie Search | O(m) | O(m) | O(m×Σ) | Trie built | Prefix searches, string sets |
Where m = string/key length, Σ = alphabet size
Decision Guide
| ├── NO | Linear Search (only option without preprocessing) |
| │ Exception | If you'll search many times, |
| ├── YES | Consider Interpolation Search |
| └── NO | Binary Search (reliable O(log n) always) |
| └── YES and function is unimodal | Ternary Search |
| └── YES | Hash Map (best average-case performance) |
| └── YES | Binary Search Tree or balanced BST |
| └── YES | Trie (O(m) per search, m = prefix length) |
Detailed Comparison by Scenario
Scenario 1 — One-time search in unsorted data
Winner: Linear Search — no preprocessing, immediate answer.
Scenario 2 — Repeated searches in static sorted data
For 1 search: both equivalent. For 1000 searches: hash map wins dramatically.
Winner: Hash Map for frequent queries. Binary Search when memory is limited.
Scenario 3 — Dynamic data (insertions and deletions mixed with searches)
from sortedcontainers import SortedList # Python library
sl = SortedList()
sl.add(5); sl.add(3); sl.add(8)
idx = sl.index(5) # O(log n) search maintained automaticallyWinner: Balanced BST (SortedList, TreeSet in Java, std::set in C++).
Scenario 4 — Prefix-based string searching (autocomplete)
| Query: "prog" | find all strings starting with "prog" |
| Linear search | check all n strings, O(n×m) total |
| Trie: traverse root | p→r→o→g in 4 steps, then enumerate children |
Winner: Trie by a large margin.
Scenario 5 — Finding minimum in rotated sorted array
Winner: Modified Binary Search — O(log n), exploits sorted structure.
Common Mistakes in Choosing Search Algorithm
Mistake 1 — Using linear search on sorted data
Mistake 2 — Using list .index() (O(n)) when hash map would be O(1)
Mistake 3 — Not accounting for preprocessing cost
# If you sort ONCE for binary search: O(n log n) + O(log n) per query
# Total for k queries: O(n log n + k log n)
# If you use hash map: O(n) build + O(1) per query
# Total for k queries: O(n + k)
# For k = 1: binary search total = O(n log n + log n) = O(n log n)
# hash map total = O(n + 1) = O(n)
# → Hash map wins even for 1 query!
# For k = 0 (just searching once, no preprocessing): linear search O(n) winsMemory-Performance Tradeoff Summary
| Binary │ O(log n) │ O(1) │ Sort | O(n lgn)│ |
| Hash Map │ O(1) average │ O(n) │ Build | O(n) │ |
| BST (bal.) │ O(log n) │ O(n) │ Build | O(n lgn│ |
| Trie │ O(m) per query │ O(n×Σ) │ Build | O(n×m) │ |
Summary — The Three Main Choices
Use Linear Search when:
- Data is unsorted and searching only once
- n is small (< 100)
- Memory is extremely tight
- Searching by complex condition
Use Binary Search when:
- Data is already sorted (or can be sorted once)
- Memory efficiency matters
- Worst-case guarantee of O(log n) required
Use Hash Map when:
- Exact key lookups are frequent
- O(n) extra memory is acceptable
- O(1) average lookup is required
Everything else — dynamic sorted data → BST; prefix searches → Trie; unimodal optimization → Ternary Search.
*Next Lesson: Searching Interview Problems*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Search Algorithm Comparison — Choosing the Right Search for Every Situation.
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, search, comparison
Related Data Structures & Algorithms Topics