Created
February 24, 2013 02:31
-
-
Save charlespunk/5022313 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order. | |
1 5 8 9 | |
2 6 8 10 |
This file contains hidden or 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
public static void merg(int[] A, int lengthA, int[] B){ | |
int current = lengthA + B.length - 1; | |
int poA = lengthA - 1; | |
int poB = B.length - 1; | |
while(poA >= 0 && poB >= 0){ | |
if(A[poA] > B[poB]) A[current--] = A[poA--]; | |
else A[current--] = B[poB--]; | |
} | |
while(poB >=0){ | |
A[current--] = B[poB--]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment