Advanced Placement (AP)

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

* appears in nums between nums[lo] and nums[hi], inclusive;

* otherwise, returns -1.

* Precondition: nums is sorted in ascending order.

* lo >= 0, hi < nums. length, nums. length > 0

*/

public static int bSearch(int[] nums, int lo, int hi, int target)

{

if (hi >= lo)

{

int mid = (lo + hi) / 2;

if (nums[mid] == target)

{

return mid;

}

if (nums[mid] > target)

{

return bSearch(nums, lo, mid - 1, target);

}

else

{

return bSearch(nums, mid + 1, hi, target);

}

}

return -1;

}

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

int target = 3;

int[] nums = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};

int targetIndex = bSearch(nums, 0, nums. length - 1, target);

How many times will bSearch be called as a result of executing the code segment above?

1

1
A

2

2
B

3

3
C

4

4
D

5

5
E

Submit

answer
Answers: 3

Other questions on the subject: Advanced Placement (AP)

image
Advanced Placement (AP), 25.06.2019 01:30, farashka03
Which two organisms in the food web would most likely be affected by a decrease in producers, or the plants, at the bottom base of the food web?
Answers: 1
image
Advanced Placement (AP), 25.06.2019 02:00, connermichaela
98 points! include an answer and in advance!
Answers: 1
image
Advanced Placement (AP), 25.06.2019 19:00, KindaSmartPersonn
Environmental science jobs are expected to grow by how much between now and 2016? a) 5% b) 15% c) 25% d) 100%
Answers: 1
image
Advanced Placement (AP), 25.06.2019 20:00, phavion
Long-term alcohol use can cause major depression. a. true b. false
Answers: 2
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
History, 17.12.2020 22:00
Konu
Arts, 17.12.2020 22:00
Konu
Spanish, 17.12.2020 22:00