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 (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m andn respectively.
public class Solution {
public void merge(int A[], int m, int B[], int n) {
int index = 0;
if(n == 0){
return;
}
else if(m == 0){
for(int q = 0; q < n; q++)
A[q] = B[q];
}
for(int i = 0; i < m ; i ++){
if(index == n)
break;
else if(A[i + index] > B[index]){
for(int j = m - 1 + index; j >= i + index; j -- ){
A[j + 1] = A[j];
}//end for
A[i + index] = B[index];
index ++;
i--;
}//end if
}//end for
if(index < n){
while(index < n){
A[m + index] = B[index];
index ++;
}
}
}
}