Binary Search (Recursive) in C++
Write binary search recursively in C++, where each call checks mid and recurses on one half. Explains the base case, the call stack, and the loop version.
1#include <iostream>2using namespace std;34int binarySearchRecursive(int arr[], int left, int right, int key) {5 if (left <= right) {6 int mid = left + (right - left) / 2;78 if (arr[mid] == key) {9 return mid; // Found at index mid10 }1112 if (arr[mid] > key) {13 return binarySearchRecursive(arr, left, mid - 1, key);14 }1516 return binarySearchRecursive(arr, mid + 1, right, key);17 }1819 return -1; // Not found20}2122int main() {23 int arr[] = {11, 12, 22, 25, 34, 64, 90};24 int n = sizeof(arr) / sizeof(arr[0]);25 int key;2627 cout << "Sorted array: ";28 for (int i = 0; i < n; i++) {29 cout << arr[i] << " ";30 }31 cout << endl;3233 cout << "Enter element to search: ";34 cin >> key;3536 int result = binarySearchRecursive(arr, 0, n - 1, key);3738 if (result != -1) {39 cout << "Element found at index: " << result << endl;40 } else {41 cout << "Element not found in array" << endl;42 }4344 return 0;45}
Sample Output
Element found at index: 3
First call found it.
In this one
- How recursion expresses halving the range
- What the base case left > right means
- How return passes the answer back up
- Why recursion depth is only about log n
How it works
- 1
Call with left 0, right 6.
- 2
mid 3 holds 25: return 3.
- 3
For other keys, call again on one half.
- 4
If left > right, return -1.
Edge cases
Missing return
Write binarySearchRecursive(...) without return.
The result of the inner call is thrown away and the function falls off the end, which is undefined behavior.
Unsorted array
Pass unsorted data.
Like the loop version, it may miss keys that are present.
What's going on
The program searches for an element in a sorted array using Binary Search (recursive version). For example:
The recursive version divides the problem into smaller subproblems by calling itself with a smaller search interval.
Header File Used
#include <iostream>
This header provides:
Understanding Recursive Binary Search
Algorithm Concept:
Visual Example:
Array: [11, 12, 22, 25, 34, 64, 90]
Search for: 25
Call 1: binarySearch(arr, 0, 6, 25)
Example (searching for 34):
Call 1: binarySearch(arr, 0, 6, 34)
Call 2: binarySearch(arr, 4, 6, 34)
Call 3: binarySearch(arr, 4, 4, 34)
Function: binarySearchRecursive()
int binarySearchRecursive(int arr[], int left, int right, int key) {
if (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid; // Found at index mid
}
if (arr[mid] > key) {
return binarySearchRecursive(arr, left, mid - 1, key);
}
return binarySearchRecursive(arr, mid + 1, right, key);
}
return -1; // Not found
}
How it works:
Step-by-Step Recursion
Base Case:
Recursive Cases:
Recursion Tree (searching for 34):
binarySearch(arr, 0, 6, 34)
mid=3, arr[3]=25 < 34
→ binarySearch(arr, 4, 6, 34)
mid=5, arr[5]=64 > 34
→ binarySearch(arr, 4, 4, 34)
mid=4, arr[4]=34 == 34
→ return 4
Time and Space Complexity
Time Complexity: O(log n)
Space Complexity: O(log n)
When to Use Recursive Binary Search
Best For:
Not Recommended For:
Recursive vs Iterative
Recursive Advantages:
Recursive Disadvantages:
Iterative Advantages:
Iterative Disadvantages:
Important Considerations
Base Case:
Recursive Calls:
Stack Depth:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Recursion depth for 1024 elements, worst case?
Pick an answer to check it.
Question 2
What returns -1?
Pick an answer to check it.
Questions people ask
Stack overflow risk?
Depth is about log₂ n, so even a billion elements need only about 30 calls.
Which version is better?
The loop avoids call overhead; the recursive one mirrors the definition. Both are fine.
What is the base case?
left > right means the range is empty.