Skip to content

Instantly share code, notes, and snippets.

@Thingyiot
Created June 26, 2015 16:16
Show Gist options
  • Save Thingyiot/1e6008f1a2f4daf6c361 to your computer and use it in GitHub Desktop.
Save Thingyiot/1e6008f1a2f4daf6c361 to your computer and use it in GitHub Desktop.
Merge two Sorted Arrays
(function mergeSort(array1, array2) {
var i = 0;
var j = 0;
var result = [];
while (i <= array1.length && j <= array2.length) {
if (array1[i] > array2[j]) {
if (typeof array2[j] !== 'undefined') {
result.push(array2[j]);
}
if (typeof array1[i] !== 'undefined') {
result.push(array1[i]);
}
} else {
if (typeof array1[i] !== 'undefined') {
result.push(array1[i]);
}
if (typeof array2[j] !== 'undefined') {
result.push(array2[j]);
}
}
++i;
++j;
}
return result;
})([0, 3, 5, 7], [1, 2, 4, 6, 8]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment