Advanced Placement (AP)

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.

answer
Answers: 1

Other questions on the subject: Advanced Placement (AP)

image
Advanced Placement (AP), 21.06.2019 18:00, anonymousanon
Nonverbal communication may best be defined as
Answers: 2
image
Advanced Placement (AP), 22.06.2019 03:30, sharpeyennifer
What does collaboration mean? ? and what is your favorite food? ?
Answers: 2
image
Advanced Placement (AP), 24.06.2019 06:00, mismhan01
What is the bottle's speed, as a percentage of the ball's incoming speed?
Answers: 3
image
Advanced Placement (AP), 24.06.2019 07:00, tangia
Gretta needs a $300,000 mortgage and is offered two choices. the monthly payments for the fixed rate and predicted payments for the arm are shown. adjustable rate mortgage year monthly payment 1-15 $1,610.46 adjustable rate mortgage year monthly payment 1-5 $1,520.06 6-15 $1,728.54 what is the difference between the total costs of the mortgages rounded to the nearest hundred? $8,700 $1,700 $15,800 $5,000
Answers: 1
Do you know the correct answer?
Consider the following method, which implements a recursive binary search. /** Returns an index in...

Questions in other subjects:

Konu
Mathematics, 16.07.2019 21:20
Konu
Mathematics, 16.07.2019 21:20