Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Created March 15, 2013 19:36
Show Gist options
  • Save Ray1988/5172511 to your computer and use it in GitHub Desktop.
Save Ray1988/5172511 to your computer and use it in GitHub Desktop.
merge sorted array b in sorted array a, assume a has enough space to hold b in the end.
int[] merge(int[] a, int[] b){
int aEnd=a.length-1;
int bEnd=b.length-1;
int mergedEnd=aEnd+bEnd+1;
while(aEnd>=0&&bEnd>=0){
if(a[aEnd]=b[bEnd]){
a[mergeEnd]=a[aEnd];
aEnd--;
bEnd--;
mergedEnd--;
}
else if(a[End]>b[End]){
a[mergedEnd]=a[aEnd];
aEnd--;
mergedEnd--;
}
else{
a[mergedEnd]=b[bEnd];
bEnd--;
mergedEnd--
}
}
while(bEnd>=0){
a[mergedEnd--]=b[bEnd--];
}
return a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment