I was just reviewing some merge sorting and finding that I'm missing how when merging it is storing the sorted arrays.
import java.util.Arrays;
public class MergeTwoArrays2 {
public static void main(String[] args) {
int a[] = { 10, 20, 30 };
int b[] = { 40, 50, 60, 70, 80 };
// determining length of both arrays
int a1 = a.length;
int b1 = b.length;
// resultant array size
int c1 = a1 + b1;
// Creating a new array
int[] c = new int[c1];
// Loop to store the elements of first
// array into resultant array
for (int i = 0; i < a1; i = i + 1) {
// Storing the elements in
// the resultant array
c[i] = a[i];
}
// Loop to concat the elements of second
// array into resultant array
for (int i = 0; i < b1; i = i + 1) {
// Storing the elements in the
// resultant array
c[a1 + i] = b[i];
}
System.out.println("" + Arrays.toString(c));
}
}
int[] c
gets populated step-by-step. Also, by changing10
to100
in your input data you will see that the output is, in fact, not sorted. See How to debug small programs for much more (and much better!) guidance.