Created
August 18, 2016 21:15
-
-
Save lizadaly/035569f9a6a442831bf1dad8c63ff63d to your computer and use it in GitHub Desktop.
sorted array without sort
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
*/ | |
var sortedArrays = function (arr1, arr2) { | |
let ans = new Array(arr1.length + arr2.length) | |
let i = 0, j = 0, k = 0 | |
while (i < arr1.length && j < arr2.length) { | |
if (arr1[i] < arr2[j]) { | |
ans[k] = arr1[i] | |
i++ | |
} | |
else { | |
ans[k] = arr2[j] | |
j++ | |
} | |
k++ | |
} | |
while (i < arr1.length) { | |
ans[k] = arr1[i] | |
i++ | |
k++ | |
} | |
while (j < arr2.length) { | |
ans[k] = arr2[j] | |
j++ | |
k++ | |
} | |
return ans | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment