Recursive Searching and Sorting (Binary Search, Merge Sort)

AP Computer Science A· difficulty 3/5

public static int bs(int[] a, int lo, int hi, int t) {
  if (lo > hi) return -1;
  int mid = (lo + hi) / 2;
  if (a[mid] == t) return mid;
  if (a[mid] < t) return bs(a, mid+1, hi, t);
  return bs(a, lo, mid-1, t);
}
// call: bs({2,4,6,8,10}, 0, 4, 8)

What does the call return?

  • A

    2

  • B

    -1

  • C

    4

  • D

    3

    check_circle

Explanation

Recursive binary search finds 8 at index 3.

Want 10 more like this — adaptive to your weak spots?

Related questions