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

this is a JAVA question consider the following instance variable and method. pri

ID: 3836714 • Letter: T

Question

this is a JAVA question

consider the following instance variable and method.

private int[] arr;

/* arr contains no duplicates, the elements in arr are in ascending order, low an int value such that 0<= low<= arr.length, high an int value such that low-1<= high< arr.length*/

public int mystery(int low, int high, int num){

int mid = (low+high)/2;

if(low>high) return low;

else if (arr[mid] < num) return mystery(mid +1, high, num);

else if (arr[mid]>num) return mystery(low, mid-1, num);

else // arr[mid] == num

return mid;}

What is returned by the call mystery(0, arr.length-1, num)?

A. The number of elements in arr that are less than num

B. the number of elements in arr that are less than or equal to num

C. the number of elements in arr that are equal to num

D. the number of elements in arr that are greater than num

E. the index of the middle element in arr

Explanation / Answer

Given code represents Binary Search Algorithm.

1.   The search array is initially the entire array.

2.   At each iteration, binary search algorithm compares the middle element in the array to the target element.

3.   As the array is sorted, algorithm eliminate half of the array. Repeat the same process until index of the target value is found or entire array is reached

Hence the correct option is (E)