Question: Write a program to test the method binarySearch. Use either the method insertionSort or selectionSort to sort the list before the search. binarySearch public static

Write a program to test the method binarySearch. Use either the method insertionSort or selectionSort to sort the list before the search.

binarySearch

public static int binarySearch(int[] list, int listLength, int searchItem)

{

int first = 0;

int last = listLength - 1;

int mid;

boolean found = false;

while (first <= last && !found)

{ mid = (first + last) / 2;

if (list[mid] == searchItem) found = true;

else if (list[mid] > searchItem) last = mid - 1;

else first = mid + 1;

}

if (found) return mid; else return -1;

}//end binarySearch

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!