AP Computer Science A Quiz
20 questions · no time limit
0:00
DATA COLLECTIONS
Question 1 of 20
No time limit
Current
Answered
Flagged
Not seen
For this recursive merge sort, what is the base case that stops further splitting of the array?
public class Sorter {
public static void mergeSort(int[] nums, int left, int right) {
// Base case: one element (or empty) segment is already sorted
if (left >= right) {
return;
}
int mid = (left + right) / 2;
// Recursive step: sort both halves
mergeSort(nums, left, mid);
mergeSort(nums, mid + 1, right);
// Merge sorted halves
merge(nums, left, mid, right);
}
private static void merge(int[] nums, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
// Merge elements in sorted order
while (i <= mid && j <= right) {
if (nums[i] <= nums[j]) {
temp[k++] = nums[i++];
} else {
temp[k++] = nums[j++];
}
}
while (i <= mid) {
temp[k++] = nums[i++];
}
while (j <= right) {
temp[k++] = nums[j++];
}
// Copy back
for (int t = 0; t < temp.length; t++) {
nums[left + t] = temp[t];
}
}
} YOUR PROGRESS
20
QUESTIONS
Ready when you are
Answer in any order. Your progress shows here as you go.
QUESTIONS
KEYBOARD
Pick an answer ABCD
Next question ↵→
Previous ←
Flag for review F
Answers save as you go · flagged questions come back at the end