Given a sorted array arr[] of n elements, find the index of a value x, or return -1 if it is not there. Binary search looks at the middle element and throws away the half that cannot contain x, so each comparison cuts the search space in half. That takes the cost from O(n) for linear search down to O(log n).
Think of looking up a word in a printed dictionary. You do not start at page 1. You open near the middle, see whether your word comes before or after, and repeat on the half that is left. Binary search does the same with array indexes:
low to the first index and high to the last.low <= high, look at the middle index mid.arr[mid] == x, you are done: return mid.arr[mid] < x, the target can only be to the right, so set low = mid + 1.high = mid - 1.low > high, nothing is left to search: return -1.The whole method depends on the array being sorted. Comparing with the middle element tells you which half to keep only because everything to its left is smaller and everything to its right is larger.
Each comparison halves the number of candidates: n, then n/2, then n/4, and so on until one is left. The number of halvings it takes to get from n down to 1 is about log₂ n. So the worst case is ⌊log₂ n⌋ + 1 comparisons, while linear search may need all n.
| Array size n | Linear search, worst case | Binary search, worst case |
|---|---|---|
| 10 | 10 | 4 |
| 1,000 | 1,000 | 10 |
| 1,000,000 | 1,000,000 | 20 |
| 1,000,000,000 | 1,000,000,000 | 30 |
Doubling the array adds just one comparison. The catch is the precondition: the data must already be sorted. Sorting costs O(n log n), so it pays off when you search the same data many times, or when the data arrives already in order.
Both versions do the same work. The iterative version keeps low and high in a loop and uses O(1) extra memory. The recursive version passes the range [l, r] to a new call each time, which matches the divide-and-conquer description closely. Its call stack grows by one frame per halving, so it uses O(log n) memory. Switch the app above to Recursive to watch the frames pile up and then return.
Loop while low <= high. When low == high there is still one element left to check. For example, searching {10, 20} for 20 ends with low and high both at index 1, and that element still has to be compared. When the loop ends without finding x, return -1.
Compute mid safely. (low + high) / 2 is correct in math, but in C++ the sum can overflow an int once the indexes pass about 1.07 billion. low + (high - low) / 2 gives the same answer and cannot overflow. Both versions in the app use it.
Pass the array by reference. A function that takes vector<int> v copies the whole vector on every call, an O(n) cost that hides the O(log n) search. Take const vector<int>& v instead.
4 (60), then 7 (130), then 5 (80), then 6 (110). Four comparisons. Linear search would need seven. Run it above to check.
About 500 after one, and about 125 after three. After ten comparisons only one candidate is left, which is why the worst case for 1,000 is 10.
Binary search can throw away the half that holds x. Try 40 10 3 4 2 by hand and search for 40: mid lands on 3, 3 < 40, so it searches the right side and never sees the 40 at index 0. (The app sorts any list you type, so it cannot show this failure.)
Best case: x is exactly at the first mid, one comparison, O(1). A missing value always runs until low > high, which is the worst case of about log₂ n + 1 comparisons.
When the data is unsorted and you search it only once or twice, since sorting first costs more than a linear scan. Also for very small arrays, where the simpler loop is just as fast in practice.