Binary Search Recursive Approach in DSA Python - Time & Space Complexity
We want to understand how the time taken by recursive binary search changes as the list size grows.
Specifically, how many steps does it take to find a number in a sorted list?
Analyze the time complexity of the following code snippet.
def binary_search(arr, target, low, high):
if low > high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search(arr, target, low, mid - 1)
else:
return binary_search(arr, target, mid + 1, high)
This code searches for a target value in a sorted list by repeatedly dividing the search range in half.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call that halves the search range.
- How many times: At most, the function calls itself until the search range is empty, roughly halving the size each time.
Each recursive call cuts the list size roughly in half, so the number of steps grows slowly as the list gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one extra step.
Time Complexity: O(log n)
This means the number of steps grows slowly, increasing by one step each time the list size doubles.
[X] Wrong: "Binary search checks every element one by one, so it is O(n)."
[OK] Correct: Binary search does not check every element; it cuts the search space in half each time, so it takes far fewer steps.
Understanding binary search's time complexity shows you how to efficiently search sorted data, a key skill in many coding challenges and real-world problems.
"What if we changed the recursive binary search to an iterative version? How would the time complexity change?"
Practice
binary search recursive approach?Solution
Step 1: Understand binary search logic
Binary search works by dividing the list into two halves and deciding which half to search next based on comparison with the middle element.Step 2: Recognize recursive approach
The recursive approach calls the same function on the half where the target could be, reducing the search space each time.Final Answer:
Divide the list into halves and search recursively in the half where the target may exist -> Option CQuick Check:
Binary search = divide and conquer [OK]
- Searching all elements linearly instead of dividing
- Not reducing the search space correctly
- Confusing iterative with recursive approach
Solution
Step 1: Identify base case condition
The base case occurs when the search space is empty, meaning low index is greater than high index.Step 2: Confirm correct return value
When low > high, the target is not found, so return -1 to indicate failure.Final Answer:
if low > high: return -1 -> Option AQuick Check:
Empty search space means return -1 [OK]
- Returning mid when low == high without checking target
- Using wrong comparison operators in base case
- Returning wrong index or value in base case
def binary_search(arr, low, high, target):
if low > high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search(arr, low, mid - 1, target)
else:
return binary_search(arr, mid + 1, high, target)
arr = [2, 4, 6, 8, 10]
print(binary_search(arr, 0, len(arr) - 1, 6))Solution
Step 1: Trace initial call
Call binary_search(arr, 0, 4, 6). mid = (0+4)//2 = 2. arr[2] = 6 matches target 6.Step 2: Return mid index
Since arr[mid] == target, function returns 2 immediately.Final Answer:
2 -> Option BQuick Check:
Target 6 found at index 2 [OK]
- Calculating mid incorrectly
- Returning wrong index
- Confusing target value with index
def binary_search(arr, low, high, target):
if low >= high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search(arr, low, mid - 1, target)
else:
return binary_search(arr, mid + 1, high, target)Solution
Step 1: Analyze base case condition
The base case incorrectly uses 'if low >= high', which misses the case when low == high and target is at that index.Step 2: Correct base case to 'if low > high'
Changing to 'if low > high' allows checking the last element when low == high before returning -1.Final Answer:
Base case condition should be 'if low > high' instead of 'if low >= high' -> Option DQuick Check:
Base case must allow checking last element [OK]
- Using >= instead of > in base case
- Incorrect mid calculation
- Returning wrong value on failure
arr = [1, 3, 5, 7, 9, 11, 13], write a recursive binary search function to find the index of the target 10. What will be the final output?Solution
Step 1: Trace recursive calls for target 10
Initial call: low=0, high=6, mid=3, arr[3]=7 < 10, search right half (4 to 6). Next mid=5, arr[5]=11 > 10, search left half (4 to 4). Next mid=4, arr[4]=9 < 10, search right half (5 to 4). Now low=5 > high=4, base case reached.Step 2: Return -1 as target not found
Since low > high, function returns -1 indicating target 10 is not in the list.Final Answer:
-1 -> Option AQuick Check:
Target 10 not in list returns -1 [OK]
- Assuming closest smaller element index is returned
- Not handling base case properly
- Confusing index with value
Ready to prove it?
Pass the test and this pattern moves unknown
