Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created February 24, 2013 02:31
Show Gist options
  • Save charlespunk/5022313 to your computer and use it in GitHub Desktop.
Save charlespunk/5022313 to your computer and use it in GitHub Desktop.
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
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