Skip to content

Instantly share code, notes, and snippets.

@bittib
Created May 26, 2013 15:04
Show Gist options
  • Save bittib/5653042 to your computer and use it in GitHub Desktop.
Save bittib/5653042 to your computer and use it in GitHub Desktop.
Merge Two Sorted Arrays Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.
public void merge(int A[], int m, int B[], int n) {
int k = n + m - 1;
while (m > 0 && n > 0){
if (A[m-1] > B[n-1])
A[k--] = A[--m];
else
A[k--] = B[--n];
}
while (n > 0)
A[k--] = B[--n];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment