合并两个有序数组

 public static void main(String args[]) {
        merge(new int[]{-1, 0, 1, 3, 4}, 5, new int[]{7, 8, 9, 10, 11}, 5);
    }

public static void merge(int A[], int firstSize, int B[], int secondSize) {
        // B合并到A 中
        int mergeSize = firstSize + secondSize;
        int[] mergeArr = new int[mergeSize];
        int j = 0;
        int merge = 0;
        for (int i = 0; i < firstSize; i++) {
            int firstCurrent = A[i];
            int secondCurrent = B[j];
            while (secondCurrent <= firstCurrent && j <= (secondSize - 1)) {
                mergeArr[merge] = secondCurrent;
                secondCurrent = B[++j];
                merge++;
            }
            mergeArr[merge] = firstCurrent;
            merge++;
            while (i == (firstSize - 1) && j <= (secondSize - 1)) {
                mergeArr[merge] = B[j];
                j++;
                merge++;
            }
        }
        for (int dkd : mergeArr) {
            System.out.print(dkd);
            System.out.print(",");
        }
    }

你可能感兴趣的:(合并两个有序数组)