subject

Consider the following method, which implements a recursive binary search. /** Returns an index in arr where target appears, if target appears

* in arr between arr[low] and arr[high], inclusive;

* otherwise, returns -1.

* Precondition: arr is sorted in ascending order.

* low >= 0, high < arr. length, arr. length > 0

*/

public static int binarySearch(int[] arr, int low, int high, int target)

{

if (low > high)

{

return -1;

}

int middle = (low + high) / 2;

if (target == arr[middle])

{

return middle;

}

else if (target < arr[middle])

{

return binarySearch(arr, low, middle - 1, target);

}

else

{

return binarySearch(arr, middle + 1, high, target);

}

}

The following code segment appears in a method in the same class as binarySearch.

int[] arr = {2, 3, 12, 34, 54};

int result = binarySearch(arr, 0, arr. length - 1, 5);

If the first call to binarySearch is the call in the code segment above, with low = 0 and high = 4, which, if any, of the following shows the values of low and high when binarySearch is called for the third time?

A. low = 0, high = 1

B. low = 0, high = 2

C. low = 1, high = 1

D. low = 2, high = 1

E. The method returns to the calling code segment before the third call to binarySearch.

ansver
Answers: 1

Another question on Advanced Placement (AP)

question
Advanced Placement (AP), 23.06.2019 00:30
70 ! in 2-3 paragraphs of 250-300 words, compare and contrast parallel and relative keys. give clear definitions and specific examples to support your answer.
Answers: 2
question
Advanced Placement (AP), 23.06.2019 02:00
How does this passage demonstrate the use of propaganda? select two options.
Answers: 3
question
Advanced Placement (AP), 23.06.2019 05:30
If you could have one clothing item of your choice, but it was in either of these colors, which would you choose out of each category? blue or pink red or yellow red or green green or purple blue or green
Answers: 1
question
Advanced Placement (AP), 25.06.2019 19:30
Marcy is a very good math student, but she seldom studies for tests. as a result, her grade has dropped this semester. this is an example wasting talent getting distracted allowing others to set goals
Answers: 1
You know the right answer?
Consider the following method, which implements a recursive binary search. /** Returns an index in...
Questions
Questions on the website: 13722367