Nobody looks up a word in a dictionary by reading from the first page. The reader opens it near the middle, sees whether the word lies before or after that page, and discards half the book at once. Binary search applies this idea to a sorted array.
The Requirement
Definition: Binary search. A search method for a sorted array that compares the key with the middle element and then continues in the half that can still contain the key, repeating until the key is found or no elements remain.
The array must be sorted. The decision to discard a half relies entirely on the order of the elements. Applied to an unsorted array, binary search may report that a value is absent when it is in fact present.
The Method
Two variables, low and high, mark the ends of the part of the array still being searched. At the start, low is 0 and high is n − 1.
- Compute the middle index:
mid = (low + high) / 2. - If
a[mid]equals the key, the search is successful. - If the key is smaller than
a[mid], it can lie only to the left: sethigh = mid - 1. - If the key is larger than
a[mid], it can lie only to the right: setlow = mid + 1. - Repeat while
low <= high. If low becomes greater than high, the key is absent.
Tracing a Search
Table 4.7 traces the search for the key 23 in the sorted array 2, 5, 8, 12, 16, 23, 38, 45, 56, 72, which has indices 0 to 9.
Table 4.7: Binary search for the key 23
| Step | low | high | mid | a[mid] | Decision |
|---|---|---|---|---|---|
| 1 | 0 | 9 | 4 | 16 | 23 > 16, so low = 5 |
| 2 | 5 | 9 | 7 | 45 | 23 < 45, so high = 6 |
| 3 | 5 | 6 | 5 | 23 | Found at index 5 |
Only three comparisons were needed, where a sequential search would have needed six.
Efficiency
Each comparison halves the number of elements still to be searched. An array of n elements can be halved only about log₂ n times before one element remains. For 1,000 elements at most 10 comparisons are needed, and for 10,00,000 elements at most 20. A sequential search of a million elements may need a million.
Sequential and Binary Search Compared
- Order of data: sequential search works on any array; binary search needs a sorted array.
- Comparisons in the worst case: n for sequential search; about log₂ n for binary search.
- Simplicity: sequential search is simpler to write.
- Best use: sequential search for small or unsorted data; binary search for large sorted data that is searched often.
If the data is unsorted and will be searched only once, sorting it first may cost more than a single sequential search.
Example 4.7: Binary search in a sorted array
Problem: Search the sorted array 2, 5, 8, 12, 16, 23, 38, 45, 56, 72 for a key entered by the user, and display the number of comparisons made.
Solution:
- Set low to 0 and high to 9.
- Repeat while low ≤ high: find mid, compare, and move low or high.
- Count each pass of the loop.
#include <stdio.h>
int main(void)
{
int a[10] = {2, 5, 8, 12, 16, 23, 38, 45, 56, 72};
int low = 0, high = 9, mid, key, pos = -1, steps = 0;
printf("Enter the value to search: ");
scanf("%d", &key);
while (low <= high)
{
mid = (low + high) / 2;
steps++;
if (a[mid] == key)
{
pos = mid;
break;
}
else if (key < a[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
if (pos == -1)
{
printf("\n%d is not in the array\n", key);
}
else
{
printf("\n%d found at index %d\n", key, pos);
}
printf("Comparisons made: %d\n", steps);
return 0;
}
Input:
23
Output:
Enter the value to search:
23 found at index 5
Comparisons made: 3
Result: The output agrees with Table 4.7: 23 is found at index 5 in three steps.
Common mistakes
- Applying binary search to an array that is not sorted.
- Writing
high = midorlow = mid, which can make the loop repeat for ever. - Using
low < highas the loop condition, which fails to examine the last remaining element.
Key takeaways
- Binary search works only on a sorted array.
- Each step compares the key with the middle element and discards half of the remaining part.
- The search continues while low ≤ high.
- About log₂ n comparisons are enough for n elements.
Practice questions
- What condition must an array satisfy before binary search can be used?
- In an array with low = 0 and high = 9, what is the first value of mid?
- About how many comparisons does binary search need for 1,000 elements in the worst case?
Answers to the practice questions
- It must be sorted.
- (0 + 9) / 2 = 4.
- About 10.
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV
