Question: /*****Modify the recursive implementation of binary search from the code below, so that the search method workd on any array of type Comparable[].*******/ Test The

/*****Modify the recursive implementation of binary search from the code below, so that the search method workd on any array of type Comparable[].*******/

Test The implentation with arrays of different types to see if it works.

- Be sure you search for items in all parts of the array and for items not in the array

- use an array of strings as one of your options

- print your arrays before searching so it's easy to see if the item is there.

- you decide how you want to input your data -- hardcore it, read it from the keyboard, read it from a file

public class BinarySearch { public static int search(int[] a, int first, int last, int key) { int result = 0; if(first>last) result = -1; else { int mid = (first + last)/2; if(key == a[mid]) result = mid; else if(key < a[mid]) result = search(a, first, mid -1, key); else if(key > a[mid]) result = search(a, mid + 1, last, key); } return result; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!