Question: package Chap2; public class BinarySearch { /** Use binary search to find the key in the list */ public static int binarySearch(int[] list, int key)
package Chap2; public class BinarySearch { /** Use binary search to find the key in the list */ public static int binarySearch(int[] list, int key) { int low = 0; int high = list.length - 1; while (high >= low) { int mid = (low + high) / 2; if (key < list[mid]) high = mid - 1; else if (key == list[mid]) return mid; else low = mid + 1; } return -low - 1; // Now high < low } }
" Convert the binarySearch method in the attached above to use Generic type T which should work for any objects that has Comparable Interface implemented.
Hint: Please use below for the method definition.
public static
create array list and sorted it out for this code to work "
Please solve this problem with java and provide code with output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
