Recursive Binary Search Algorithm
Binary Search is a searching algorithm used to find the position of a
specific element in a sorted array. It works by repeatedly dividing the
search interval in half. The algorithm compares the target element with
the middle element of the array. If they are equal, the target has been
found. If the target is smaller than the middle element, the search
continues in the left half of the array; otherwise, it continues in the right
half.
The recursive binary search works in a similar way but uses recursion
to keep dividing the array into smaller halves.
Recursive Binary Search Algorithm (Pseudocode)
Let's define the recursive binary search algorithm:
Algorithm RecursiveBinarySearch(arr, left, right, target):
if left > right:
return -1 // Target not found
mid = left + (right - left) / 2
if arr[mid] == target:
return mid // Target found, return the index
else if arr[mid] > target:
return RecursiveBinarySearch(arr, left, mid - 1, target) // Search in
the left half
else:
return RecursiveBinarySearch(arr, mid + 1, right, target) // Search in
the right half
Explanation of the Algorithm:
1. Base Case: If the left index exceeds the right index (left > right), it
means the target is not in the array, so we return -1.
2. Find the Middle Element: Calculate the middle index mid = left +
(right - left) / 2.
3. Compare with the Target:
o If the middle element arr[mid] equals the target, return mid
(the index of the target).
o If arr[mid] > target, the target must be in the left half, so the
search continues with the left half of the array (left to mid-1).
o If arr[mid] < target, the target must be in the right half, so the
search continues with the right half of the array (mid+1 to
right).
Example:
Let's say we have an array arr = [2, 5, 8, 12, 16, 23, 38] and we want to
search for the target value 16.
1. Initial call: RecursiveBinarySearch(arr, 0, 6, 16)
o left = 0, right = 6, mid = (0 + 6) / 2 = 3
o arr[3] = 12, which is less than 16, so search in the right half:
RecursiveBinarySearch(arr, 4, 6, 16)
2. Next call: RecursiveBinarySearch(arr, 4, 6, 16)
o left = 4, right = 6, mid = (4 + 6) / 2 = 5
o arr[5] = 23, which is greater than 16, so search in the left half:
RecursiveBinarySearch(arr, 4, 4, 16)
3. Next call: RecursiveBinarySearch(arr, 4, 4, 16)
o left = 4, right = 4, mid = (4 + 4) / 2 = 4
o arr[4] = 16, which matches the target, so return 4.
Thus, the target 16 is found at index 4 of the array.
Worst Case Time Complexity of Recursive Binary Search:
The time complexity of binary search depends on how many times we
can divide the search interval by half before we find the target or
determine that it's not in the array.
In the worst case:
At each step, we eliminate half of the remaining elements, so the
size of the problem reduces exponentially.
After kk steps, the search space reduces to n2k\frac{n}{2^k},
where nn is the size of the array.
The algorithm terminates when the search space is reduced to 1 element,
i.e., n2k=1\frac{n}{2^k} = 1. Solving for kk gives:
n2k=1⇒2k=n⇒k=log2(n)\frac{n}{2^k} = 1 \quad \Rightarrow \quad 2^k
= n \quad \Rightarrow \quad k = \log_2(n)
Therefore, the worst-case time complexity is:
O(logn)O(\log n)
Space Complexity:
Since the algorithm is recursive, the space complexity is determined by
the depth of the recursion stack. In the worst case, the depth of recursion
is proportional to the number of times the array is divided in half. This
gives a space complexity of:
O(logn)O(\log n)
Summary:
Time Complexity (Worst Case): O(logn)O(\log n), because the
problem size is halved with each recursive call.
Space Complexity (Worst Case): O(logn)O(\log n), due to the
recursive call stack.
Recursive binary search is highly efficient for searching in a sorted array,
with logarithmic time complexity.