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

Other questions on the subject: Advanced Placement (AP)

image
Advanced Placement (AP), 23.06.2019 11:30, paigefields2578
Marian is itemizing deductions on her federal income tax return and had $1500 in non-reimbursed work expenses last year. if her agi was $46,000, and if non-reimbursed work expenses are deductible to the extent that they exceed 2 % of a taxpayer's agi, how much can marian deduct for non- reimbursed work expenses? a: $30 b: $580 c: $920 d: $1470
Answers: 1
image
Advanced Placement (AP), 23.06.2019 18:00, gg68814
Firms use marginal analysis to determine prices by a) changing prices in accordance with information from retailers. b) using comparisons with strong competitors to position prices accordingly. c) examining the effect on revenue of small changes to the price of one item. d) trying to minimize production costs by using better and more efficient methods.
Answers: 2
image
Advanced Placement (AP), 24.06.2019 03:30, naomicervero
Which of the following is a type of technology that can be used at a coal-burning power plant to remove both particulate matter and gases? bag filter catalytic converter cyclone electrostatic precipitator scrubber
Answers: 3
image
Advanced Placement (AP), 24.06.2019 09:30, biggasi
Since gaining independence from spain and portugal, many latin american nations have faced similar struggles. choose a common issue discussed in this lesson, such as the influence of military dictatorships, struggles with democracy, or continuing revolutions and coups. in a short paragraph, discuss how that struggle has affected two latin american nations.
Answers: 3
You know the right answer?
Consider the following method, which implements a recursive binary search. /** Returns an index in...

Questions in other subjects:

Konu
Chemistry, 07.02.2021 03:10
Konu
Mathematics, 07.02.2021 03:10
Konu
Mathematics, 07.02.2021 03:10