Given two sorted integer arrays A and B, merge B into A as one sorted array.

Note:
You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

public class Solution {
    public void merge(int A[], int m, int B[], int n) {
        int inserted = 0;
        int i=0;
        if(B==null||B.length==0){
            return;
        }
        while(i<m){
            if(A[i]>=B[0]){
                int temp = A[i];
                A[i] = B[0];
                B[0] = temp;
                int j=0;
                while(j+1<n&&B[j]>B[j+1]){
                    temp = B[j];
                    B[j] = B[j+1];
                    B[j+1] = temp;
                    j++;
                }
            }
            i++;
        }
        int j=0;
        while(j<n){
            A[i++]=B[j++];
        }
    }
}

中心思想:

有一个迭代器遍历A里所有的元素,然后和B里面的第一个元素比较,如果A的元素大于B的元素,那么交换,

始终保证B的第一个元素大于A当前遍历到的元素,并且保证B是有序的。最后把B衔接到A的末尾就行。

算法的复杂度目测是O(n*m)的