Created
April 1, 2017 17:29
-
-
Save lupomontero/899d480c9ee5fd69504e002ce73be33f 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
// | |
// 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