Recursive Searching and Sorting (Binary Search, Merge Sort)

AP Computer Science A· difficulty 4/5

public static void sort(int[] a, int lo, int hi) {
    if (lo >= hi) return;
    int mid = (lo + hi) / 2;
    sort(a, lo, mid);
    sort(a, mid + 1, hi);
    // merge halves...
}

Which is the correct base case for this mergesort skeleton?

  • A

    lo >= hi (single element or empty)

    check_circle
  • B

    lo == 0

  • C

    hi == a.length

  • D

    lo + hi == 0

Explanation

A single (or empty) range is already sorted, so recursion stops when lo >= hi.

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

Related questions