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.

Respuesta :

Answer:

D. low = 2, high = 1

Explanation:

Middle is 2

First call: low = 0 high = 1

Middle is 0

Second call: low = 1 high = 1

Middle is 2

Third call: low = 2 high = 1

A technique of identifying a problem (or a set of strategies) in elements of (a simplified version of) itself is known as recursion. therefore, the final answer is "low = 1, high = 1".

Recursion:

  • In the given code, an integer array that is "arr" holds an integer value.
  • In the next step, an integer variable "result" is declared that calls the binarySearch method.
  • In the first time calls Recursion 1, when low = 0 and high = 4 its mid = 2, therefore arr[2] = 12 > 5 = high = mid-1
  • In the second time calls Recursion 2, when low = 0 and high = 1 its mid =0, therefore arr[0] = 2 < 5 = low = mid+1
  • In the third time calls Recursion 3, when low = 1 and high = 1 its mid =1, therefore arr[1] = 3 < 5 = element not found

Therefore, the final answer is "Option C", that is "low = 1, high = 1".

Find out more information about the Recursion here:

brainly.com/question/17298888