Created
April 22, 2015 01:54
-
-
Save assertchris/e5b3848ab6d8a5252462 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
function merge(data) { | |
var length = data.length; | |
if (length === 1) { | |
return [data[0]]; | |
} | |
if (length === 2) { | |
if (data[0] > data[1]) { | |
return [data[1], data[0]]; | |
} | |
return [data[0], data[1]]; | |
} | |
var first = merge(data.slice(0, Math.ceil(length / 2))); | |
var second = merge(data.slice(first.length)); | |
var result = []; | |
while (first.length && second.length) { | |
if (first[0] > second[0]) { | |
result.push(second.shift()); | |
} else { | |
result.push(first.shift()); | |
} | |
} | |
return result.concat(first, second); | |
} | |
merge([10, 5, 4, 6, 8, 2, 1, 0, 3, 9, 7]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment