C++ Program for Binary Search with Example and Explanation
while (low <= high) and updates low or high until the target is found or not; example: int binarySearch(int arr[], int size, int target) { int low = 0, high = size - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; }.Examples
How to Think About It
Algorithm
Code
#include <iostream> using namespace std; int binarySearch(int arr[], int size, int target) { int low = 0, high = size - 1; while (low <= high) { int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) low = mid + 1; else high = mid - 1; } return -1; } int main() { int arr[] = {1, 3, 5, 7, 9}; int size = sizeof(arr) / sizeof(arr[0]); int target = 5; int result = binarySearch(arr, size, target); cout << result << endl; return 0; }
Dry Run
Let's trace searching for 5 in the array {1, 3, 5, 7, 9} through the code.
Initialize pointers
low = 0, high = 4 (array indices)
Calculate mid
mid = 0 + (4 - 0) / 2 = 2
Compare arr[mid] with target
arr[2] = 5 equals target 5, return 2
| low | high | mid | arr[mid] | comparison |
|---|---|---|---|---|
| 0 | 4 | 2 | 5 | arr[mid] == target, return 2 |
Why This Works
Step 1: Divide and conquer
The code splits the search space in half each time using mid, which makes searching faster than checking every element.
Step 2: Adjust search range
If the middle element is less than the target, the code moves low up to search the right half; otherwise, it moves high down to search the left half.
Step 3: Stop condition
The loop stops when low passes high, meaning the target is not in the array, and returns -1.
Alternative Approaches
#include <iostream> using namespace std; int binarySearchRecursive(int arr[], int low, int high, int target) { if (low > high) return -1; int mid = low + (high - low) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) return binarySearchRecursive(arr, mid + 1, high, target); else return binarySearchRecursive(arr, low, mid - 1, target); } int main() { int arr[] = {1, 3, 5, 7, 9}; int size = sizeof(arr) / sizeof(arr[0]); int target = 5; int result = binarySearchRecursive(arr, 0, size - 1, target); cout << result << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; int main() { int arr[] = {1, 3, 5, 7, 9}; int size = sizeof(arr) / sizeof(arr[0]); int target = 5; bool found = binary_search(arr, arr + size, target); cout << (found ? "Found" : "Not Found") << endl; return 0; }
Complexity: O(log n) time, O(1) space
Time Complexity
Binary search divides the search space in half each step, so it runs in logarithmic time, O(log n), where n is the number of elements.
Space Complexity
The iterative version uses constant extra space O(1) because it only stores a few variables.
Which Approach is Fastest?
Iterative binary search is fastest and uses less memory than recursive. Using std::binary_search is convenient but does not give the index.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative Binary Search | O(log n) | O(1) | Fastest, low memory |
| Recursive Binary Search | O(log n) | O(log n) | Clear code, uses call stack |
| std::binary_search (STL) | O(log n) | O(1) | Quick check if element exists |
low or high pointers correctly, causing infinite loops or wrong results.