What if you could find a name in a phone book without flipping every page?
Why Binary Search Recursive Approach in DSA C?
Imagine you have a huge phone book and you want to find a friend's number. You start from the first page and look at every name one by one until you find it.
This way is very slow and tiring because you have to check many names before finding the right one. If the book is very big, it can take a long time and you might lose your place or make mistakes.
Binary search uses a smart way to find the name quickly by always looking at the middle of the list and deciding which half to search next. The recursive approach does this by calling itself on smaller parts until it finds the name or knows it is not there.
int linear_search(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) return i;
}
return -1;
}int binary_search(int arr[], int left, int right, int target) {
if (left > right) return -1;
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] > target) return binary_search(arr, left, mid - 1, target);
else return binary_search(arr, mid + 1, right, target);
}This approach lets you find items in sorted lists very fast, even if the list is huge.
When you search for a word in a dictionary app, it quickly jumps to the middle and narrows down the search instead of reading every word.
Manual searching checks every item one by one, which is slow.
Binary search splits the list and searches halves recursively, making it fast.
Useful for quickly finding items in sorted lists or arrays.
Practice
binary search recursive approach when searching for an element in a sorted array?Solution
Step 1: Understand binary search principle
Binary search works by dividing the sorted array into two halves and deciding which half to search next based on comparison with the middle element.Step 2: Apply recursion in binary search
The recursive approach calls the same function on the half where the target element might be, reducing the search space each time.Final Answer:
Divide the array into halves and search recursively in the half where the element can be -> Option BQuick Check:
Binary search = divide and conquer [OK]
- Thinking binary search checks every element
- Not using recursion to reduce search space
- Searching unsorted arrays with binary search
Solution
Step 1: Understand base case in recursion
The base case stops recursion when the search space is invalid, meaning low index is greater than high index.Step 2: Identify correct condition
When low > high, it means the element is not found, so return -1 to indicate failure.Final Answer:
if (low > high) return -1; -> Option CQuick Check:
Base case = low > high [OK]
- Using low == high as base case incorrectly
- Returning wrong value in base case
- Not handling base case leading to infinite recursion
int binarySearch(int arr[], int low, int high, int x) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == x) return mid;
else if (arr[mid] > x) return binarySearch(arr, low, mid - 1, x);
else return binarySearch(arr, mid + 1, high, x);
}
int main() {
int arr[] = {2, 4, 6, 8, 10};
int n = 5;
int result = binarySearch(arr, 0, n - 1, 6);
printf("%d", result);
return 0;
}Solution
Step 1: Trace the recursive calls
Initial call: low=0, high=4, mid=2, arr[2]=6 equals target 6, so return 2 immediately.Step 2: Understand output
The function returns index 2 where element 6 is found, so printf prints 2.Final Answer:
2 -> Option AQuick Check:
Element 6 found at index 2 [OK]
- Confusing index with value
- Off-by-one errors in mid calculation
- Not returning correct index on match
int binarySearch(int arr[], int low, int high, int x) {
if (low >= high) return -1;
int mid = (low + high) / 2;
if (arr[mid] == x) return mid;
else if (arr[mid] > x) return binarySearch(arr, low, mid - 1, x);
else return binarySearch(arr, mid + 1, high, x);
}Solution
Step 1: Analyze base case condition
The base case uses if (low >= high) which incorrectly stops search when low equals high, missing the last element.Step 2: Correct base case
It should be if (low > high) to allow checking when low equals high, ensuring all elements are searched.Final Answer:
Change base case to if (low > high) return -1; -> Option DQuick Check:
Base case must allow low == high [OK]
- Stopping recursion too early
- Incorrect mid calculation causing infinite recursion
- Returning wrong index on match
arr = {1, 3, 5, 7, 9, 11, 13}, you want to find the index of element 8 using recursive binary search. What will be the sequence of mid indices checked before concluding the element is not found?Solution
Step 1: Calculate first mid index
Initial low=0, high=6, mid = (0+6)/2 = 3, arr[3]=7 less than 8, search right half next.Step 2: Calculate second mid index
New low=4, high=6, mid = (4+6)/2 = 5, arr[5]=11 greater than 8, search left half next.Step 3: Calculate third mid index
New low=4, high=4, mid = (4+4)/2 = 4, arr[4]=9 greater than 8, search left half next low=4, high=3 ends recursion.Final Answer:
[3, 5, 4] -> Option AQuick Check:
Mid indices checked = 3, 5, 4 [OK]
- Not updating low and high correctly
- Miscomputing mid index
- Stopping search too early
Ready to prove it?
Pass the test and this pattern moves unknown
