Created
June 26, 2015 16:16
-
-
Save Thingyiot/1e6008f1a2f4daf6c361 to your computer and use it in GitHub Desktop.
Merge two 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
(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