Question: fined the error in this code, then correct it so it will run. import java.util.*; public class Assignment111{ public static void main (String [] args){

fined the error in this code, then correct it so it will run.
import java.util.*;
public class Assignment111{
public static void main (String [] args){
int ar [] = new int [100];
int keys [] = new int [50];
int LinearSteps [] = new int [50];
int BinarySteps [] = new int [50];
int max= 10;
int min= 1;
for (int i = 0; i < ar.length; i++) {
ar[i] = (int) (Math.random()*(max-1+1)+min);
System.out.print(ar[i]+" ");
}
System.out.println(" ");
for (int i = 0; i < keys.length; i++) {
keys[i] = (int) (Math.random()*(max-1+1)+min);
System.out.print(ar[i]+" ");
}
LinearSearchKeys(ar,keys,LinearSteps);
SortingMethod(ar);
BinarySearchKeys(ar,keys,BinarySteps);
//Casess
int minL=0;
for(int i=0; i
if(LinearSteps[i]
minL=LinearSteps[i]; }
}
System.out.println("Best case in linear: " + minL);
int maxL=0;
for(int i=0; i
if(LinearSteps[i]>maxL) {
maxL=LinearSteps[i]; }
}
System.out.println("Worst case in linear: " + maxL);
int sum=0;
for(int i=0; i
sum+=LinearSteps[i];
}
System.out.println("Average case in linear: " + (maxL/50));
int minB=0;
for(int i=0; i
if(LinearSteps[i]
minB=BinarySteps[i]; }
}
System.out.println("Best case in binary: " + minL);
int maxB=0;
for(int i=0; i
if(LinearSteps[i]>maxB) {
maxB=BinarySteps[i]; }
}
System.out.println("Worst case in binary: " + maxL);
int sumB=0;
for(int i=0; i
sumB+=BinarySteps[i];
}
System.out.println("Average case in binary: " + (maxL/50));
}
public static void SortingMethod(int ar[]){
for (int i = 0; i < ar.length; i++) {
int currentMin = ar[i];
int currentMinIndex = i;
for (int j = i+1; j < ar.length; j++) {
if (currentMin > ar[j]) {
currentMin = ar[j];
currentMinIndex = j;
}
}
if(currentMinIndex != i){
ar[currentMinIndex]=ar[i];
ar[i]=currentMin;
}
}
for(int i=0;i
System.out.print(ar[i]+" ");
}
}
public static void BinarySearchKeys(int keys[], int ar[], int BinarySteps[]){
int Bsteps=0;
for(int i=0;i
int low=0;
int high=(ar.length-1);
while(low<=high){
Bsteps++;
int mid=((low+high)/2);
if(keys[i]>ar[mid]){
low=mid+1;
}
else if(ar[mid]>keys[i]){
high=mid-1;}
else { break; }
}
BinarySteps[i]=Bsteps;
}
}
public static void LinearSearchKeys(int keys[], int ar[],int [] LinearSteps){
for(int i=0;i
int Lsteps=0;
for(int j=0;j
Lsteps++;
if(keys[j]==ar[i]){ break; }
}
LinearSteps[i]=Lsteps;
}
}
}
1-create an array of size 100.
2- generate random value between 1 and 10.
3-generate and array called keys of 50( in the range of 1 to 10 randomly)
4-then search for the keys( in the first array) using both binary and linear search, fined each key and the time the algorithm took to fined the results, then print the best, average, and worst case for both search algorithm in terms of number of steps and time needed to fined the solution.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!