Merge Method Array Manipulation
What is the output of the merge function call in this program?
public class MergeExample {
public static void merge(int[] a, int start, int mid, int end) {
int n1 = mid - start + 1;
int n2 = end - mid;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++)
L[i] = a[start + i];
for (int j = 0; j < n2; j++)
R[j] = a[mid + 1 + j];
int i = 0, j = 0, k = start;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k++] = L[i++];
} else {
a[k++] = R[j++];
}
}
while (i < n1) {
a[k++] = L[i++];
}
while (j < n2) {
a[k++] = R[j++];
}
}
public static void main(String[] args) {
int[] a = {3, 27, 38, 43, 9, 10, 82};
merge(a, 0, 3, a.length - 1);
System.out.println(a[0] + " " + a[4]);
}
}
A
3 43
B
3 27
C
27 38
D
3 38
Question Leaderboard
Not enough data yet to show leaderboard.
APFIVE