Recursive Binary Search Algorithm
Recursive Binary Search Algorithm
The recursive binary search works in a similar way but uses recursion
to keep dividing the array into smaller halves.
if arr[mid] == target:
else:
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.
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.
o arr[3] = 12, which is less than 16, so search in the right half:
RecursiveBinarySearch(arr, 4, 6, 16)
o arr[5] = 23, which is greater than 16, so search in the left half:
RecursiveBinarySearch(arr, 4, 4, 16)
O(logn)O(\log n)
Space Complexity:
O(logn)O(\log n)
Summary: