Question: Shown below is the iterative version of binary search. Write the recursive version. Note that the initial call to the recursive version would look like
Shown below is the iterative version of binary search. Write the recursive version.
Note that the initial call to the recursive version would look like this:
int result = binarySearch(array, target, 0, array.length-1);
// Iterative version
public static int binarySearch(Comparable [] array, Comparable target)
{
int first=0, last=array.length-1, mid;
while (first <= last) { mid = (first + last) / 2; if (array[mid].compareTo(target) < 0)
first = mid + 1; else if (array[mid].compareTo(target) > 0)
last = mid - 1; else return mid; }//end while
return -1; //not found } //end binarySearch
// Recursive version...
public static int binarySearch(Comparable[] ara, Comparable target, int low, int high)
{
} // End binary search, recursive...
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
