Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote