Skip to content

Instantly share code, notes, and snippets.

@assertchris
Created April 22, 2015 01:54
Show Gist options
  • Save assertchris/e5b3848ab6d8a5252462 to your computer and use it in GitHub Desktop.
Save assertchris/e5b3848ab6d8a5252462 to your computer and use it in GitHub Desktop.
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