Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started