Binary
Search
Master the divide-and-conquer strategy that finds any element in a sorted list in \( O(\log n) \) time: exponentially faster than checking every item.
What Is Searching?
Searching means locating a specific value (called the target) inside a collection of data. Think of looking up a word in a dictionary or finding a contact on your phone.
The Phone Book Analogy
Imagine looking for "Martinez" in a phone book with 1,000 pages. You wouldn't start at page 1 and flip one page at a time. Instead you:
Why Sorted Order Matters
Binary search can only work on sorted data. When the middle value is too large, everything to its right is also too large: we can safely ignore that half. This guarantee breaks if the data is unsorted.
The Binary Search Algorithm
Efficiency: \( O(\log_2 n) \)
Linear vs Binary Search
| Linear Search | Binary Search | |
|---|---|---|
| Requires sorted data? | No | Yes |
| Best case | \( O(1) \) | \( O(1) \) |
| Worst case | \( O(n) \) | \( O(\log n) \) |
| 1,000 elements (worst) | 1,000 steps | 10 steps |
| Implementation | Simple loop | Slightly more complex |
| When to use | Small or unsorted data | Large, sorted data |
Binary Search in Pseudocode
How to Apply Binary Search
Example 1: Find 23
A sorted collection makes discarded search ranges meaningful
Sorted order makes it possible to discard an entire search range after one comparison. An unsorted collection does not support the same elimination.Binary search with AP-style list indices
This example uses AP-style list positions 1 through 9. Searching for 23 tests the midpoint values 17, 31 and 23 and returns position 6. The list is sorted before searching.Count comparisons as the list grows
For inclusive midpoint binary search on n ≥ 1 items, the maximum number of probes is floor(log₂ n) + 1. The horizontal axis uses a log₂ scale; the count measures search probes after sorting.Example 2: Find 50 (Not Found)
List: [2, 8, 14, 27, 33, 41, 56, 72]. Target: 50.
Example 3: 1 Million Items
Suppose you have a sorted database of 1,000,000 records and you need to find one specific record.
The Halving Pattern
Common Mistakes
Binary Search Summary
| Property | Detail |
|---|---|
| Precondition | Data must be sorted |
| Strategy | Divide and conquer: halve repeatedly |
| Best case | \( O(1) \) : target is at mid on first check |
| Worst case | \( O(\log_2 n) \) |
| Max steps for \( n = 1024 \) | 10 |
| Max steps for \( n = 1{,}000{,}000 \) | 20 |
| When NOT to use | Unsorted data, very small lists |
Question 1 of 5
A sorted list has 512 elements. What is the maximum number of comparisons binary search needs to find a target (or determine it's not present)?
Question 2 of 5
Which of the following is a necessary precondition for binary search to work correctly?
Question 3 of 5
You binary search the sorted list [1, 4, 9, 16, 25, 36, 49] for the value 36. Which element is checked first?
Question 4 of 5
A sorted list has 2,000 elements. Approximately how many times faster is binary search than linear search in the worst case?
Question 5 of 5
During binary search, if the target is less than the middle element, what happens next?