Skip to content

Instantly share code, notes, and snippets.

@lupomontero
Created April 1, 2017 17:29
Show Gist options
  • Save lupomontero/899d480c9ee5fd69504e002ce73be33f to your computer and use it in GitHub Desktop.
Save lupomontero/899d480c9ee5fd69504e002ce73be33f to your computer and use it in GitHub Desktop.
//
// Buen algoritmo de ordenado.
//
// Big O => n * log(n);
//
'use strict';
function merge(left, right) {
var out = [];
while (left.length && right.length) {
out.push(left[0] < right[0] ? left.shift() : right.shift());
}
while (left.length) {
out.push(left.shift());
}
while (right.length) {
out.push(right.shift());
}
return out;
}
function mergeSort(data) {
if (data.length < 2) {
return data;
}
var midPoint = Math.round(data.length / 2);
return merge(
mergeSort(data.slice(0, midPoint)),
mergeSort(data.slice(midPoint))
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment