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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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

}

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
Transcribed Image 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
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY