Skip to content

Instantly share code, notes, and snippets.

@linfongi
Created April 17, 2016 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linfongi/c79e79ac78738e7b9278d34b5c76a450 to your computer and use it in GitHub Desktop.
Save linfongi/c79e79ac78738e7b9278d34b5c76a450 to your computer and use it in GitHub Desktop.
function mergeSort(arr) {
if (arr.length < 2) return arr;
var mid = (arr.length - 1) >> 1;
var l = mergeSort(arr.slice(0, mid + 1));
var r = mergeSort(arr.slice(mid + 1));
return merge(l, r);
}
function merge(s1, s2) {
var result = [];
while (s1.length && s2.length) {
result.push(s1[0] < s2[0] ?
s1.shift() :
s2.shift());
}
return result.concat(s1.length ? s1 : s2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment