Created
April 21, 2020 16:25
-
-
Save alexy4744/3504edd073475ceddfc1ad1c251c286e to your computer and use it in GitHub Desktop.
merge 2 sorted arrays
This file contains 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 int[] merge(int[] a, int[] b) { | |
int[] c = new int[a.length + b.length]; | |
int i = 0; | |
int j = 0; | |
int k = 0; | |
while (i < a.length && j < b.length) { | |
if (a[i] < b[j]) c[k++] = a[i++]; | |
else c[k++] = b[j++]; | |
} | |
while (i < a.length) c[k++] = a[i++]; | |
while (j < b.length) c[k++] = b[j++]; | |
return c; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment