Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Lab #6 Section 1 – Chapter 11 Write a program class called BinarySearch that has a static method called Search that searches the array a for key. If key is not in the array segment, then -1 is...

1 answer below »
Lab #6
Section 1 – Chapter 11
Write a program class called BinarySearch that has a static method called Search that searches
the a
ay a for key. If key is not in the a
ay segment, then -1 is returned. Otherwise returns an
index in the segment such that key == a[index]. Precondition: a[first] <= a[first + 1]<= ... <=
a[last]. The search method takes an int a
ay, index of first number in a
ay, index of last
number in a
ay and key to search for in the a
ay.
Section 2 – Chapter 12
Write a Main program that uses the BinarySearch class you created in Section 1 to search for a
value. Your program will print key + " is at index " + result) if it finds the value or key + " is not in
the a
ay” if it does not find the value.
Answered Same Day Nov 08, 2021

Solution

Neha answered on Nov 09 2021
158 Votes
import java.util.A
ays;
import java.util.Scanner;
public class Main{
public static int binarySearch(int a
[], int first, int last, int key){
if (last>=first){
int mid = first + (last - first)/2;
if (a
[mid] == key){
return mid;
}
if (a
[mid] > key){
return binarySearch(a
, first, mid-1, key);
search in left suba
ay
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here