Algorithms

Binary Search

๐Ÿ“‹ Quick Reference

PropertyValue
TypeDivide and conquer search algorithm
TimeO(log n)
SpaceO(1) iterative, O(log n) recursive
PrerequisiteArray must be sorted
Best ForFinding elements in sorted arrays, finding boundaries
One-liner: Halves the search space each step by comparing with the middle element.

๐ŸŽฎ Interactive Visualizer

Watch how Binary Search eliminates half the search space with each comparison:

Loading visualizer...
Try these operations:
  1. Search for an element that exists - count the comparisons
  2. Search for an element that doesn't exist - see where it would be
  3. Notice: array of 1000 elements needs only ~10 comparisons!

๐Ÿ”ง Key Implementations

JAVA(18 lines)
Code
Loading syntax highlighter...

Find First Occurrence (Lower Bound)

JAVA(16 lines)
Code
Loading syntax highlighter...

Find Last Occurrence (Upper Bound)

JAVA(16 lines)
Code
Loading syntax highlighter...

Java Standard Library

JAVA(10 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

ScenarioComparisonsTime
n = 10~3-4O(log n)
n = 100~6-7O(log n)
n = 1,000~10O(log n)
n = 1,000,000~20O(log n)
n = 1,000,000,000~30O(log n)

Why O(log n)?

Each step halves the search space:
n โ†’ n/2 โ†’ n/4 โ†’ n/8 โ†’ ... โ†’ 1

Number of steps = logโ‚‚(n)

Example: 1024 elements
1024 โ†’ 512 โ†’ 256 โ†’ 128 โ†’ 64 โ†’ 32 โ†’ 16 โ†’ 8 โ†’ 4 โ†’ 2 โ†’ 1
= 10 steps = logโ‚‚(1024)

Good Use Cases

  • Searching sorted arrays - classic use case
  • Finding boundaries - first/last occurrence, insertion point
  • Searching in rotated arrays - modified binary search
  • Finding peak elements - search space reduction
  • Optimization problems - "find minimum X such that..."

Avoid When

  • Unsorted data - must sort first, O(n log n)
  • Small datasets - linear search is simpler for n < 10-20
  • Frequent insertions - keeping array sorted is expensive
  • Linked lists - no O(1) random access

๐Ÿงฉ Common Patterns

Search in Rotated Sorted Array

JAVA(28 lines)
Code
Loading syntax highlighter...

Find Peak Element

JAVA(15 lines)
Code
Loading syntax highlighter...

โš ๏ธ Common Pitfalls

1. Integer Overflow

JAVA(5 lines)
Code
Loading syntax highlighter...

2. Off-by-One Errors

JAVA(17 lines)
Code
Loading syntax highlighter...

3. Forgetting Sort Requirement

JAVA(7 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: What's the time complexity of binary search?
"

O(log n) - we eliminate half the search space with each comparison. For an array of n elements, we need at most logโ‚‚(n) + 1 comparisons.

Q: Can you implement binary search without recursion?
"

Yes, the iterative version is actually preferred as it uses O(1) space vs O(log n) stack space for recursion.

Q: How do you handle duplicates?
"

Use lower_bound/upper_bound variants. Lower bound finds the first occurrence, upper bound finds the position after the last occurrence. Count of element = upper_bound - lower_bound.

Q: When is linear search better than binary search?
"

For very small arrays (n < 10-20), linear search can be faster due to simpler logic and better cache behavior. Also when array is unsorted and will only be searched once.


๐Ÿ“š Series Navigation

Previous: Part 8: EnumSet

Visualizer: BinarySearch from @tomaszjarosz/react-visualizers