1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class arraySort { public static void main(String[] args) { int arrayA[] = {1, 3, 4, 12, 55, 56, 71, 81}; int arrayB[] = {2, 12, 13, 19, 32, 55, 57, 100}; System.out.println(Arrays.toString(sumArray(arrayA,arrayB))); }
private static int[] sumArray(int[] a, int[] b) { int[] result = new int[a.length + b.length]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] <= b[j]) { result[k++] = a[i++]; } else { result[k++] = b[j++]; } }
while (i < a.length) { result[k++] = a[i++]; } while (j < b.length) { result[k++] = b[j++]; } return result; } }
|