Need the code adjusted to search for 50. With the end results matching the attached data. import java.util.Random; import java.util.Arrays; public class Lab6 { public static void main(String args[]) { int[] n= {2500, 5000, 10000, 20000, 40000}; for(int i=0; i= max) { throw new IllegalArgumentException("max must be greater than min"); } Random r = new Random(); return r.nextInt((max - min) + 1) + min; } }
Need the code adjusted to search for 50. With the end results matching the attached data.
import java.util.Random;
import java.util.Arrays;
public class Lab6 {
public static void main(String args[]) {
int[] n= {2500, 5000, 10000, 20000, 40000};
for(int i=0; i<n.length; i++) {
int[] arr1 = getRandIntArray(n[i]);
Arrays.sort(arr1);
long startTime = System.nanoTime();
// linear
linearSearch(arr1, 50);
// search 50 (this number is not in array)
// search a number outside the range (-10, 200)
// search a number that is inside the array (100)
long stopTime = System.nanoTime();
System.out.println("for array size: "+n[i]+" Execution time linear: "+(stopTime - startTime)/1000.0 + " microSec");
startTime = System.nanoTime();
// binary
binarySearch(arr1, 50);
stopTime = System.nanoTime();
System.out.println("for array size: "+n[i]+" Execution time binary: "+(stopTime - startTime)/1000.0 + " microSec");
}
}
public static void linearSearch(int[] arr, int search_item) {
for(int i=0; i<arr.length; i++) {
//System.out.println(arr[i]);
}
}
public static void binarySearch(int[] arr, int search_item) {
}
public static void printArray(int[] arr) {
for(int i=0; i<arr.length; i++) {
//System.out.println(arr[i]);
}
}
public static int[] getRandIntArray(int n) {
int[] arr = new int[n];
// Random array that doesn't contain 50
for(int i=0; i<n; i++) {
int num = getRandomNumberInRange(1,100);
if(num == 50) {
num+=1;
}
arr[i]=num;
}
return arr;
}
public static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
}


Step by step
Solved in 2 steps









