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

Hello Please use Java For the bolow question In main, use an initializer list cr

ID: 3744030 • Letter: H

Question

Hello Please use Java For the bolow question

In main, use an initializer list create an array of ints called nums holding the following values: 1,4, 4, 22, -5, 10, 21,-47, 23 Write a method, called binarvSearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point+1). Ex. Given the array values in #21 above, the array will be sorted as-47,-5.1.4.4. 10.21.22.23. The index for key of 4 will be 3. The return value when search for 6 will be the insertion point for 6 which is index of 5, so the return will be minus(5+1)-6. Create an int array of size 20, called data and write a method to fill the array with random ints [-100, 100]. Recall that random's nextlnt^x) method returns a value from 0 to x-1. The upper bound is exclusive. Modify the values to fit the required ranges. Use the binarySearch method you previously wrote to return the index of the key value searched for in data for the client to print Print the value of the key value search for and the index of the value, or the value of -(insertion noint +1)

Explanation / Answer

import java.util.*;
public class HelloWorld{

public static void main(String []args){
Scanner sc = new Scanner(System.in);
int arr[] = new int[20];
for(int i=0;i<20;i++){
arr[i] = getRandomNumber(-100,100); //storing values in array
}
Arrays.sort(arr);
System.out.println("The array of random numbers is : ");
for(int i=0;i<20;i++){
System.out.print(arr[i]+" ");
}
System.out.println(" Enter Key: ");
int key = sc.nextInt();
System.out.println("Output: "+binarySearch(arr,key));
}
//function to get random number between min and max
public static int getRandomNumber(int min,int max){
return (int)(Math.random() * ((max - min) + 1)) + min;
}
//function for binary search
public static int binarySearch(int[] arr, int key) {
int low=0;
int high = arr.length-1;
int mid = (low + high) / 2;
while (low <= high) {
mid = (low + high) / 2;
if (arr[mid] < key) {
low = mid + 1;
} else if (arr[mid] > key) {
high = mid - 1;
} else if (arr[mid] == key) {
return mid;
}
}
return -(mid+1);
}
}

SAMPLE OUTPUT:

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