Binary Search
in C++
Learn how to efficiently search sorted data by repeatedly halving the search space: from the core concept to std::binary_search in the C++ Standard Library.
What is Binary Search?
Binary search is an algorithm that finds the position of a target value within a sorted array. Instead of checking every element one by one, it eliminates half of the remaining elements at each step.
Why Does Speed Matter?
Imagine searching for a name in a phonebook with 1,000,000 entries. A linear search checks up to all one million entries. Binary search needs at most 20 checks.
Key Vocabulary
The Algorithm Step by Step
Visual Walkthrough
Search for target = 23 in the sorted array below.
Writing It in C++
Linear vs Binary Search
| Linear Search | Binary Search | |
|---|---|---|
| Time Complexity | \( O(n) \) | \( O(\log n) \) |
| Requires Sorting? | No | Yes |
| 1M Elements (worst) | 1,000,000 checks | 20 checks |
| Implementation | Simple loop | Slightly more involved |
| Best For | Small or unsorted data | Large sorted data |
How to Apply Binary Search
std::sort (\( O(n \log n) \)).std::binary_search, std::lower_bound, or std::upper_bound.Using std::binary_search
The C++ Standard Library provides std::binary_search in the <algorithm> header. It returns a simple bool: does the value exist in the sorted range?
lower_bound & upper_bound
Example 1: Basic Search
Search for target = 11 in {3, 5, 8, 11, 14, 19}.
Example 2: Not Found
Search for target = 10 in {3, 5, 8, 11, 14, 19}.
Example 3: STL in Action
Given an unsorted vector, sort it and then use the STL to check membership and find position.
Common Mistakes
low + high can exceed \( 2^{31} - 1 \). Use \( \text{low} + (\text{high} - \text{low}) / 2 \).STL Binary Search Cheat Sheet
| Function | Returns | Use When |
|---|---|---|
| std::binary_search | bool | You only need yes/no |
| std::lower_bound | iterator to \( \ge \) target | You need the position |
| std::upper_bound | iterator to \( > \) target | You need the insertion point |
| std::equal_range | pair of iterators | You need all occurrences |
Question 1 of 5
What is the most important precondition for binary search to work correctly?
Question 2 of 5
What is the time complexity of binary search on a sorted array of \( n \) elements?
Question 3 of 5
In C++, what does std::binary_search return?
std::binary_search returns a simple bool: true if the value exists in the sorted range, false otherwise. For an iterator, use std::lower_bound. For a pair, use std::equal_range.Question 4 of 5
Why do we compute mid as low + (high - low) / 2 instead of (low + high) / 2?
low and high are both large (close to \( 2^{31} - 1 \)), their sum low + high overflows an int. The expression low + (high - low) / 2 avoids this because (high - low) is always non-negative and smaller than either value.Question 5 of 5
You have the sorted array {1, 4, 7, 10, 13, 16} and search for target = 7. How many comparisons does binary search make?
arr[2], which is 7. Since this matches the target, the search is complete after just one comparison.