Write GenericSearch with two generic methods that work on an array of any type whose elements can be compared:
public static <E extends Comparable<E>> int linearSearch(E[] list, E key)
public static <E extends Comparable<E>> int binarySearch(E[] list, E key) // the list is sorted
Count the calls to compareTo in both. The input has three lines: the type (int, double or word), the sorted elements, and the keys to look for. main builds an Integer[], a Double[] or a String[] and passes it to the same methods. For the input
int
2 4 7 10 11 45 50 59 60 66 69 70 79
11 70 5 2
the program prints
11: linear search 4 (5 comparisons), binary search 4 (3 comparisons)
70: linear search 11 (12 comparisons), binary search 11 (3 comparisons)
5: linear search -1 (13 comparisons), binary search -1 (4 comparisons)
2: linear search 0 (1 comparisons), binary search 0 (3 comparisons)