Question: **JAVA** public class BinarySearch { /** Use binary search to find the key in the list */ public static int binarySearch(int[] list, int key) {

**JAVA**

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 -1 - low;

}

}

1) See the details and trace the method as intrustructed

int [] list = {2,4,7,10,11,45,50,59,60,66,69,70,79}

int i = BinarySearch.binarySearch(list,2 );

int j = BinarySearch.binarySearch(list,11);

int k = BinarySearch.binarySearch(list,12);

int l = BinarySearch.binarySearch(list,1 );

int m = BinarySearch.binarySearch(list, 3);

2)what would happen if we placed (high >= low) in line 7 with (high > low)?

3) Does the method work if there are duplicate elements in the list?

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!