Created
March 15, 2013 19:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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