题目:
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 void merge(int A[], int m, int B[], int n) {
int i = 0;
int j = 0;
int[] temp = new int[m+n];
int index = 0;
while(i<m && j<n){
if(A[i] >= B[j])
temp[index++] = B[j++];
else
temp[index++] = A[i++];
}
while(i<m)
temp[index++] = A[i++];
while(j<n)
temp[index++] = B[j++];
for(i=0;i<index;i++)
A[i] = temp[i];
}