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 mand n respectively.
public class Solution {
/**Start merging process from tail of arrays to front of array.<br>
*
* @param A --one array with has larger size
* @param m --number of elements in array A
* @param B --the other array with smaller size
* @param n --number of elements in array B
* @author Averill Zheng
* @version 2014-06-03
* @since JDK 1.7
*/
public void merge(int A[], int m, int B[], int n) {
int i = 0, j = 0;
while(i < m && j < n){
if(A[m - 1 - i] > B[n - 1 - j]){
A[m + n - 1 -(i + j)] = A[m - 1 - i];
++i;
}
else{
A[m + n - 1 -(i + j)] = B[n - 1 - j];
++j;
}
}
while(j < n){
A[n - 1 - j] = B[n - 1 - j];
++j;
}
}
}