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

14:44 1. Modify the \"BinarySearch\" program so that if the search key is in the

ID: 3742165 • Letter: 1

Question

14:44 1. Modify the "BinarySearch" program so that if the search key is in the array, it returns the largest index i for which a[i] is equal to key, but otherwise, returns-i where i is the largest index such that ai is less than key. It should also be modified to deal with integer arrays rather than string arrays. Note: The program should take two command- line arguments, (1) an input file that contains a sorted integer array; and (2) an integer to search for in that array public class BinarySearch public static int search(String key, Stringl] a) t return search(key, a 0, a.length); public static int search(String key, Stringl] a, int lo, int hi) lSearch for key in a[lo, hi). if (hi lo) return-1; int mid lo+ (hi - lo)/2; int cmp-almid].compareTo(key); if (cmp > 0) return search(key, a, lo, mid) else if (cmp

Explanation / Answer

please give thumbs up, thanks


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

/**
*
* @author VISHAL
*/
public class BinarySearch {
public static int search(int key, int[]a )
{
return search(key,a,0,a.length);
}
public static int search(int key,int[]a,int lo,int hi)
{
if(hi<=lo)
{
if(a[lo]==key)
return lo;
else
return -lo;
}
int mid=lo+(hi-lo)/2;
if(a[mid]>key)
return search(key,a,lo,mid);
else if(a[mid]<key)
return search(key,a,mid+1,hi);
else
return mid;
}
public static void main(String[]args) throws FileNotFoundException
{
int n=0;
int data[]=new int[1000];
String filename=(args[0]);
System.out.println(filename);
Scanner sc=new Scanner(new File(filename));
while(sc.hasNext())
{
data[n]=Integer.parseInt(sc.next());
n++;
}
int key=Integer.parseInt(args[1]);
if(search(key,data)<0)
{
System.out.println(key);
}
  
  
}
  
}

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