AP Computer Science A · Topic 10.2
Recursive Searching and Sorting (Binary Search, Merge Sort) Practice
Part of Recursion.
Practice questions
20
Sample questions
5 of 20 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
// mergesort on n elementsWhat is the time complexity?
- A
O(n)
- Bcheck_circle
O(n log n)
- C
O(log n)
- D
O(n^2)
Why
Mergesort divides log n levels and merges O(n) per level: O(n log n).
- A
Sample 2difficulty 2/5
// binary search on sorted array of size nWhat is the worst-case time complexity?
- A
O(n log n)
- Bcheck_circle
O(log n)
- C
O(1)
- D
O(n)
Why
Binary search halves the search space each step, giving O(log n).
- A
Sample 3difficulty 2/5
int[] a = {5, 2, 8, 1, 9}; // attempt binary search on aWhy might binary search fail on this array?
- A
Binary search needs even length
- B
The array is too small
- Ccheck_circle
The array is not sorted
- D
Arrays must be Strings
Why
Binary search requires the array to be sorted; this array is not.
- A
Sample 4difficulty 3/5
int[] left = {2, 5, 8}; int[] right = {1, 3, 7}; int[] out = new int[6]; int i = 0, j = 0, k = 0; while (i < left.length && j < right.length) { if (left[i] <= right[j]) out[k++] = left[i++]; else out[k++] = right[j++]; } while (i < left.length) out[k++] = left[i++]; while (j < right.length) out[k++] = right[j++];What is out after merging?
- Acheck_circle
{1, 2, 3, 5, 7, 8}
- B
{1, 3, 7, 2, 5, 8}
- C
{8, 7, 5, 3, 2, 1}
- D
{2, 5, 8, 1, 3, 7}
Why
The merge interleaves to produce a fully sorted array.
- A
Sample 5difficulty 3/5
int[] a = {6, 3, 8, 5, 2, 7, 4, 1}; int mid = a.length / 2; int[] left = new int[mid]; int[] right = new int[a.length - mid]; for (int i = 0; i < mid; i++) left[i] = a[i]; for (int i = mid; i < a.length; i++) right[i - mid] = a[i];What is right after the divide step?
- A
{1, 4, 7, 2}
- B
{6, 3, 8, 5}
- C
{6, 3, 8, 5, 2, 7, 4, 1}
- Dcheck_circle
{2, 7, 4, 1}
Why
The right half of an 8-element array is the second 4 elements.
- A