Java Merge Sort mergeSortedAaary(int[] a, int[] b)

Here you can find the source of mergeSortedAaary(int[] a, int[] b)

Description

merge Sorted Aaary

License

Apache License

Declaration

static Integer[] mergeSortedAaary(int[] a, int[] b) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    static Integer[] mergeSortedAaary(int[] a, int[] b) {
        int aL = a.length;
        int bL = b.length;

        Integer[] c = new Integer[aL + bL];

        int countA = 0;
        int countB = 0;
        int countC = 0;
        while (countA < aL || countB < bL) {
            if (countA < aL && countB < bL && a[countA] < b[countB]) {
                c[countC] = a[countA];/*from ww  w .ja va 2  s  . com*/
                countA++;
                countC++;
            } else if (countA < aL && countB < bL && b[countB] < a[countA]) {
                c[countC] = b[countB];
                countB++;
                countC++;
            } else if (countA < aL && countB < bL && b[countB] == a[countA]) {
                c[countC] = a[countA];
                countA++;
                countC++;
                c[countC] = b[countB];
                countB++;
                countC++;
            } else if (countA == aL) {
                c[countC] = b[countB];
                countB++;
                countC++;
            } else if (countB == bL) {
                c[countC] = a[countA];
                countA++;
                countC++;
            }
        }
        return c;
    }
}

Related

  1. mergeSort(int[] array)
  2. mergeSort(long[] theArray, int nElems)
  3. mergeSort(Object[] src, Object[] dest, int low, int high, int off)
  4. mergeSort(T[] src, T[] dst, int start, int end)
  5. mergeSorted(float[] a, int alen, float b[], int blen)
  6. mergeSortedArrays(double[] a, double[] b)