public static int[] merge(int[] a, int[] b){
int len1 = a.length;
int len2 = b.length;
int[] c = new int[len1 + len2];
int len = len1 < len2 ? len1 : len2;
int i = 0 , j = 0 , k = 0;
for(; i < len1 && j < len2 && k < len1 + len2 ; ){
if(a[i] < b[j]){
c[k] = a[i];
k++;
i++;
}else{
c[k] = b[j];
k++;
j++;
}
}
if(i < len1){
while (i != len1 || k != len1 + len2){
c[k] = a[i];
k++;
i++;
}
}
if(j < len2){
while (j != len2 || k != len1 + len2){
c[k] = b[j];
k++;
i++;
}
}
return c;
}
No comments:
Post a Comment