key: tranverse from the last index;
three point: m-1, n-1; m+n-1;
void merge(int A[], int m, int B[], int n) {
int i = m-1;
int j = n-1;
int start = m+n-1;
while(i >=0 && j >= 0){
if(A[i] < B[j]){
A[start--] = B[j--];
}else{
A[start--] = A[i--];
}
}
if(i < 0){
while(j >= 0){
A[start--] = B[j--];
}
}
}
本文介绍了一种将两个已排序数组合并为一个新排序数组的算法。该算法从两个数组的末尾开始比较,并将较大的元素放入目标数组的末尾,直至完成合并。若其中一个数组先被完全合并,则直接将另一个数组剩余部分复制到目标数组。
1466

被折叠的 条评论
为什么被折叠?



