Last active
November 2, 2017 13:28
-
-
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.
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 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