2. 1
Recursive
Binary Search
Recursive Binary Search is a search algorithm that is
used to find the position of a target value in a sorted array.
It is a recursive implementation of the binary search
algorithm, which means that it uses a function that calls
itself to search for the target value. The algorithm works
by dividing the search space in half and comparing the
middle element of the array with the target value. If the
middle element is equal to the target value, the algorithm
returns the index of the middle element. If the middle
element is greater than the target value, the algorithm
searches the left half of the array. If the middle element is
less than the target value, the algorithm searches the right
half of the array. The process is repeated until the target
value is found or the search space is exhausted
3. Sample Code
2
public class RecursiveBinarySearch {
public boolean binarySearchRecur(int[] A, int left, int right, int item) {
if (left <= right) {
int mid = left + (right - left) / 2; // finding middle index
if (A[mid] == item)
return true; // item found
else if (item < A[mid]) { // recursively search on the left
sub-array
return binarySearchRecur(A, left, mid-1, item);
}else{
// recursively search on the right sub-array
return binarySearchRecur(A, mid+1, right, item);
}
}else{
return false; // item not found )
}
}
}