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
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