Last active
May 10, 2017 18:42
-
-
Save zacacollier/493f5ac5aa9bbdd48154514e8664a0e9 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/dukekol
This file contains 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
'use strict'; | |
var seq = [1, 4, 6, 3, 1, 4, 6, 3, 25, 593, 0, 53, 24, 352, 53905, 234]; | |
function merge(array) { | |
if (array.length < 2) { | |
console.log('returns ' + array); | |
return array; | |
} | |
var mid = array.length / 2; | |
var l = array.slice(0, mid); | |
var r = array.slice(mid, array.length); | |
console.log('done'); | |
return mergeSort(merge(l), merge(r)); | |
} | |
function mergeSort(left, right) { | |
var acc = []; | |
while (left.length && right.length) { | |
if (left[0] <= right[0]) { | |
acc.push(left.shift()); | |
} else { | |
acc.push(right.shift()); | |
} | |
} | |
while (left.length) { | |
acc.push(left.shift()); | |
} | |
while (right.length) { | |
acc.push(right.shift()); | |
} | |
return acc; | |
} | |
console.log(merge(seq)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment