00 6:58 AM MQ 10: Binary Search Started: Jan 21 at 2:57pm Quiz Instructions Ques
ID: 3877335 • Letter: 0
Question
00 6:58 AM MQ 10: Binary Search Started: Jan 21 at 2:57pm Quiz Instructions Question 1 1 pts Consider the following recursive implementation of binary search on a sorted array of ints (a slight variation on the one seen in class): Courses 28 Groups public static int binarySearch(int a, int x, int low, int high) if [Low> high) { Calendar return -1: //xcan't be in an invali d range int index(owhigh)/2 int y at index); if (x y) t Inbox return index; else if (x y) return binarySearch(a, x, low, index- Help else t IIy return binarySearch(a, x, index+1, hi gh): The below code tests binarySearch by creating an array of a million monotonically increasing values (a subsequent value carn differ from its predecessor by 0, 1, or 2). Then, it times searching for 100000 values in that array. We also add up all the indexes where we find what we're looking for along with -1 values when we don't find what we're looking for. Finally, we search for the same values with binarySearch and also add either the found index or 1 if not found. The code below performs this search. You can place it in a main method. k-Explanation / Answer
The option D would be correct as the rest of the options are incorrect because they won't implement a binary search and will give wrong results.
In D we will find other index as well in which the element might be same so that the linear search sumIndex and the Binary search sumIndex is the same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.