private static int[] merge(int[] a, int[] b)
    {
        int minLength = Math.min(a.length, b.length);
        int resultSize = a.length + b.length;
        int[] result = new int[resultSize];
        int indexA,indexB, resultIndex;
        indexA = indexB = resultIndex = 0;

        while(indexA < minLength && indexB < minLength)
        {
            if(a[indexA] < b[indexB])
            {
                result[resultIndex] = a[indexA];
                indexA++;
            }
            else
            {
                result[resultIndex] = b[indexB];
                indexB++;
            }
            resultIndex++;
        }

        if(indexB < b.length)
        {
            for(int i = resultIndex; i < resultSize; i++)
                result[i] = b[indexB];
        }
        else if(indexA < a.length)
        {
            for(int i = resultIndex; i < resultSize; i++)
                result[i] = a[indexA];
        }

        return result;
    }