Skip to content

Instantly share code, notes, and snippets.

@alexy4744
Created April 21, 2020 16:25
Show Gist options
  • Save alexy4744/3504edd073475ceddfc1ad1c251c286e to your computer and use it in GitHub Desktop.
Save alexy4744/3504edd073475ceddfc1ad1c251c286e to your computer and use it in GitHub Desktop.
merge 2 sorted arrays
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