š Interpolation Search
Looking for "Smith" in a phone book? You don't open to the middle - you go near the end! That's Interpolation Search - it makes smart guesses based on the value you're searching for. For uniformly distributed data, it's even faster than Binary Search!
The Phone Book Strategy
Imagine searching for a name in a phone book:
Smart vs Naive
Binary Search: Always opens to the middle, regardless of what you're searching for.
Interpolation Search: Makes an educated guess!
- Searching for "Adams"? ā Start near beginning
- Searching for "Smith"? ā Start near end
- Searching for "Miller"? ā Start in middle
Key insight: Use the VALUE to estimate POSITION!
The Formula
Interpolation Formula
pos = left + ((target - arr[left]) / (arr[right] - arr[left])) Ć (right - left)
Intuition:
- How far is target from left value? ā
(target - arr[left]) - What's the total range of values? ā
(arr[right] - arr[left]) - Proportion Ć array length = estimated position!
The Code
Click Run to execute your code
Quick Quiz
-
Why is Interpolation Search O(log log n) for uniform data?
Show answer
For uniformly distributed data, each guess gets us very close to the target. Instead of halving the search space (log n), we reduce it much faster (log log n). Example: For 1 million elements, Binary Search needs ~20 steps, Interpolation needs ~5-6! -
When does Interpolation Search perform poorly?
Show answer
When data is NOT uniformly distributed! Example: [1, 2, 3, 4, 5, 1000, 2000, 3000]. The gap causes bad estimates, potentially O(n) worst case. Always use Binary Search if distribution is unknown!
Complexity Analysis
| Case | Time | When |
|---|---|---|
| Best | O(1) | First guess is correct |
| Average (uniform) | O(log log n) ā | Uniformly distributed data |
| Worst | O(n) | Non-uniform distribution |
| Space | O(1) | No extra space |
Interpolation vs Binary Search
| Feature | Binary Search | Interpolation Search |
|---|---|---|
| Best Case | O(1) | O(1) |
| Average (uniform) | O(log n) | O(log log n) ā |
| Worst Case | O(log n) ā | O(n) |
| Requires | Sorted array | Sorted + uniform ā |
| Predictable | Yes ā | No |
| Best For | General use ā | Uniform data ā |
When to Use
ā Use Interpolation Search When:
- Uniformly distributed data: Phone books, dictionaries
- Large datasets: 1M+ elements where O(log log n) matters
- Sequential IDs: User IDs, order numbers
- Known distribution: You know data is uniform
ā Use Binary Search Instead When:
- Unknown distribution: Can't verify uniformity
- Non-uniform data: Gaps or clusters in values
- Small datasets: < 10K elements, difference negligible
- Safety matters: Need predictable O(log n)
š” Key Takeaway
Interpolation Search is Binary Search's smarter cousin:
- ā O(log log n) for uniform data - faster than Binary!
- ā Makes educated guesses based on values
- ā Perfect for phone books, dictionaries
- ā ļø O(n) worst case for non-uniform data
- ā ļø Only use when distribution is known
Rule of thumb: If in doubt, use Binary Search!
Summary
The 3-Point Interpolation Search Guide
- Strategy: Estimate position using value proportion
- Complexity: O(log log n) for uniform, O(n) worst case
- Use when: Large uniform datasets (phone books!)
Interview tip: Mention Interpolation Search as an optimization for Binary Search when data is uniformly distributed. Explain the trade-off: faster average but worse worst-case!
What's Next?
Next: Exponential Search - for unbounded or infinite arrays!
Enjoying these tutorials?