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[]) {...


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
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
//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
//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
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;
}


}


Based on the given code, the time taken for<br>program to run the search is:<br>Searching 50 (not in array)<br>for array size: 2500 Execution time linear: 21.67 microSec<br>for array size: 2500Execution time binary: 1.971 microSec<br>for array size: 5000 Execution time linear: 34.281 microSec<br>for array size: 5000Execution time binary: 0.18 microSec<br>for array size: 10000 Execution time linear: 83.222 microSec<br>for array size: 10000Execution time binary: 0.21 microSec<br>for array size: 20000 Execution time linear: 130.494 microSec<br>for array size: 20000Execution time binary: 0.22 microSec<br>for array size: 40000 Execution time linear: 289.348 microSec<br>for array size: 40000Execution time binary: 0.231 microSec<br>

Extracted text: Based on the given code, the time taken for program to run the search is: Searching 50 (not in array) for array size: 2500 Execution time linear: 21.67 microSec for array size: 2500Execution time binary: 1.971 microSec for array size: 5000 Execution time linear: 34.281 microSec for array size: 5000Execution time binary: 0.18 microSec for array size: 10000 Execution time linear: 83.222 microSec for array size: 10000Execution time binary: 0.21 microSec for array size: 20000 Execution time linear: 130.494 microSec for array size: 20000Execution time binary: 0.22 microSec for array size: 40000 Execution time linear: 289.348 microSec for array size: 40000Execution time binary: 0.231 microSec
Jun 05, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here