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 file 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 > int binarySearchGeneric(E[] list, E key)

create array list and sorted it out for this code work

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!