Run Code Test Cases Assignment Docs Grade 7 Points Status Submitted Exercise Compare Binar Q43842599
RUN CODE TEST CASES ASSIGNMENT DOCS | GRADE MORE 7 points Status: Not Submitted In this exercise you will compare binary search and linear (or sequential) search ar We have included the algorithms below. What you need to do in this problem is modify each method to instead return the number of comparisons, A comparison is any time you are doing an == > > or <test. Then you can test out the results on lists of different sizes. We have provided a helper method to generate a list of a certain size. 8.3.4: Comparing Binary Searc… 1 import java.util. 3 public class CompareSearch extends ConsoleProgram public void run() System.out.println(“Table of comparison counts”); System.out.println(“LengthttBinary SearchtLinear Search”), testArrayOfLength(ie); testArrayOfLength(20); // This problem generates an array of length length. Then we select a random // index of that array and get the element. Then we print out the table row // entry for how many comparisons it takes on binary search and linear search. // You’ll need to update those methods. public void testArrayOfLength(int length) int[] arr = generateArrayOfLength(length); 1/System.out.println(Arrays.toString(ar)); int index = Randomizer.nextInt(length); int eles = arr[index]; System.out.println(length. tit. binarySearch(arr, elen) “elle”. Linear Search(arr, ele)); NNNNN public int[] generateArrayOfLength(int length) int() arr-new int[length); for(int i = 0; i < length; i++) arr[i] = Randonizer.nextInt(100); Arrays.sort(arr); return arr; // Do a binary search on array to find number. You’ll need to modify this wthod to return the number of comparisons done. public int binarysearchist array int number) O O OOOO3628) @ 8.3.4: Comparing Binary Searc… TO 39 42. // Do a binary search on array to find number. You’ll need to modify this 11 method to return the number of comparisons done, public int binarySearch(int[] array, int number) int low = ; int high = array.Length – 1; while (Low <= high) int mid = (low high) / 2; if (array(mid) == number) return mid; else if(array(nid) < number) low = mid + 1; high = mid – 1; return -1; // Do a linear search on array to find the index of number. You’ll need to modify // this exercise to return the number of comparisons* done. public int linear Search(int[] array, int number) for (int i = 3; i < array.length; 1++) 69 71 if (array[i] == number) return 1; // the method returns as soon as the number is found 76 return -1; // the code will get here if the number isn’t found P lom OOOOOOOOOOO Type here to search Show transcribed image text RUN CODE TEST CASES ASSIGNMENT DOCS | GRADE MORE 7 points Status: Not Submitted In this exercise you will compare binary search and linear (or sequential) search ar We have included the algorithms below. What you need to do in this problem is modify each method to instead return the number of comparisons, A comparison is any time you are doing an == > > orExpert Answer
Answer to RUN CODE TEST CASES ASSIGNMENT DOCS | GRADE MORE 7 points Status: Not Submitted In this exercise you will compare binary…
OR