Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am using Eclipse application Remember:You can remove all the variables from th

ID: 3725064 • Letter: I

Question

I am using Eclipse 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 = 105)
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

Since you ate using eclipse, I have provided the code in java

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Arrays;

import java.util.Random;

public class Test {

   public static boolean binSearch(int[] a, int key) {

      int low = 0;

      int high = a.length;

      int mid;

      while (high > low) {

          mid = (low + high)/2;

          if (a[mid] == key)

              return true;

          else if (a[mid] < key)

              low = mid + 1;

          else

              high = mid;

      }

      return false;

   }

   public static void main(String[] args) throws IOException {

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      System.out.println("Enter a number: ");

      int n = Integer.parseInt(br.readLine());

      int[] a = new int[n];

      Random rand = new Random(System.nanoTime());

      for (int i = 0 ; i < n; i++)

          a[i] = rand.nextInt(2000) - 1000;

      Arrays.sort(a);

      long startTime, stopTime;

      int key;

      startTime = System.nanoTime();

      for (int i = 0; i < 500; i ++) {

          key = rand.nextInt(2000) - 1000; //case 1
// key = rand.nextInt(2000) + 1001; //case 2 for worst case

          binSearch(a, key);

      }

      stopTime = System.nanoTime();

      long timeElapsed = stopTime - startTime;

      System.out.println("Average time required to search is: " + timeElapsed/500 + " nano seconds" );

   }

}

If you have any questions, please let me know.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote