Skip to content

Instantly share code, notes, and snippets.

@shailrshah
Last active November 2, 2017 13:28
Show Gist options
  • Save shailrshah/95ffc8c69087633a44174623a78f6681 to your computer and use it in GitHub Desktop.
Save shailrshah/95ffc8c69087633a44174623a78f6681 to your computer and use it in GitHub Desktop.
Given two sorted integer arrays a[] and b[], merge b[] into a[] as one sorted array.
public void merge(int[] a, int m, int[] b, int n) {
int i = m - 1;
int j = n - 1;
int k = m + n - 1;
while(i >= 0 && j >= 0)
a[k--] = a[i] > b[j] ? a[i--] : b[j--];
while(i >= 0)
a[k--] = a[i--];
while(j >= 0)
a[k--] = b[j--];
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment