I would prefer with Java application Remember:You can remove all the variables f
ID: 3725113 • Letter: I
Question
I would prefer with Java application
Remember:You can remove all the variables from the workspace by writing “clear”Look up the description of all the functions in MATLABby typing doc in the command window. ImplementlinearSearch(a,key)and binarySearch(a,key)functions
.Part A.
In this part we will calculate theaverage-case running time of each function.
1.Request the user to enter a positive integer, and call it n.(n = 10^5)
2.Generate nrandom integersbetween -1000to1000and save them in arraya.(You can use randi function in MATLAB)
3.Sort a(you can use any sorting algorithm you want. If you are using MATLAB, you can call the sortfunctionto sort your numbers)
4.Pick a random numberinaand save it invariable calledkey.
5.Call eachfunction separately to search for the keyin the given array.
6.To calculatethe average-running time, you need to have a timer to save the total runtime when repeating step 4 and 5 for 500 times. (tic toc in MATLAB)
(Note1: Do not forget to divide the runtime by the number of the times you run step 4-5)
(Note2: Remember to choose a different random number each time you go back to step 4.)
Part B.
In this part we willcalculate theworst-case running time of each function.
1.Repeat steps 1 to 3inpart A.
2.Now to havethe worst-casescenario, set the value of the key to5000to makesure it does not exist in the array.
3.Run each function once to calculatethe worst-case running time whenn=105.
4.Calculate how much time your machine takes to run onesingle step. (Hint: look at HW3)
5.Now estimatethe worst-case running timeforeach algorithm when n=107.(Hint: look at HW3)
6.Now repeat step 1-3 forn = 107and compare the actual running time with youranswer in step 5.
Explanation / Answer
Please find my code for Part A.
Please repost others in separate post.
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class LinearSearchTime {
// This function returns index of element x in arr[]
static int linearSearch(int arr[], int x)
{
for (int i = 0; i < arr.length; i++)
{
// Return the index of the element if the element
// is found
if (arr[i] == x)
return i;
}
// return -1 if the element is not found
return -1;
}
// A iterative binary search function. It returns
// location of x in given array arr[l..r] if present,
// otherwise -1
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length-1;
while (l <= r)
{
int m = l + (r-l)/2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was
// not present
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n value: ");
int n = sc.nextInt();
int[] arr = new int[n];
Random rn = new Random();
int r = 1000 - (-1000) + 1;
for(int j=0; j<n; j++) {
int randomNum = rn.nextInt() % r + (-1000); // -1000 to 1000
arr[j] = randomNum;
}
Arrays.sort(arr);
//picking a random number on range 0 to n-1
long totalTimeLinear = 0,totalTimeBinary = 0;
long start, end;
for(int i=1; i<=500; i++) {
int key = arr[rn.nextInt(n)];
start = System.currentTimeMillis();
linearSearch(arr, key);
end = System.currentTimeMillis();
totalTimeLinear = totalTimeLinear + (end - start);
start = System.currentTimeMillis();
binarySearch(arr, key);
end = System.currentTimeMillis();
totalTimeBinary = totalTimeBinary + (end - start);
}
System.out.println("Average time in linear search: "+(totalTimeLinear/500.0));
System.out.println("Average time in binary search: "+(totalTimeBinary/500.0));
}
}
/*
Sample run:
Enter n value: 1000000
Average time in linear search: 0.208
Average time in binary search: 0.002
*/