Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

What statements did you use? In what tone of voice?

Answered: 1 week ago

Question

Refer to Example 10.18 and find a 90% confidence interval for /0.

Answered: 1 week ago