| preferred AP College board partner for AP classes
Login
 
Register
AP Computer Science A/Unit 6: Array
Start Practice Test
Share
Recursive Binary Search Execution
< Prev
Next >

What output does the binary search method produce when searching for 8?

public class BinarySearchRec {
    public static int binarySearch(int[] arr, int low, int high, int target) {
         if(low > high) return -1;
         int mid = (low + high) / 2;
         if(arr[mid] == target) return mid;
         else if(arr[mid] > target)
             return binarySearch(arr, low, mid - 1, target);
         else 
             return binarySearch(arr, mid + 1, high, target);
    }
    public static void main(String[] args) {
         int[] sorted = {2, 4, 6, 8, 10, 12};
         System.out.println(binarySearch(sorted, 0, sorted.length - 1, 8));
    }
}
A

3

B

4

C

-1

D

2

Check Answer
APFIVE © 2020.
Email: [email protected]|Privacy Policy