Write a program to test the method binarySearch. Use either the method insertion
ID: 3730596 • Letter: W
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 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
Explanation / Answer
import java.util.Scanner; public class BSearchSelectionSort { public static int binarySearch(int[] list, int listLength, int searchItem) { int first = 0; int last = listLength - 1; int mid = 0; boolean found = false; while (first searchItem) last = mid - 1; else first = mid + 1; } if (found) return mid; else return -1; }//end binarySearch public static void selectionSort(int arr[]) { for (int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.