Created
March 30, 2015 06:44
-
-
Save mistadikay/07d29ab24eb191587d6b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let mergeSort = (arr) => { | |
if (arr.length < 2) return arr; | |
let middle = parseInt(arr.length / 2), | |
left = arr.slice(0, middle), | |
right = arr.slice(middle); | |
return merge(mergeSort(left), mergeSort(right)); | |
} | |
let merge = (left, right) => { | |
let result = []; | |
while (left.length && right.length) { | |
left[0] <= right[0] ? | |
result.push(left.shift()) : | |
result.push(right.shift()); | |
} | |
while (left.length) result.push(left.shift()); | |
while (right.length) result.push(right.shift()); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment