Skip to content

Instantly share code, notes, and snippets.

@mrspeaker
Created March 19, 2012 09:35
Show Gist options
  • Save mrspeaker/2105288 to your computer and use it in GitHub Desktop.
Save mrspeaker/2105288 to your computer and use it in GitHub Desktop.
JavaScript merge sort
function merge(a, b, c) {
c = c || [];
if(!a.length && !b.length) return c;
c.push(a[0] < b[0] || isNaN(b[0]) ? a.shift() : b.shift());
return merge(a, b, c);
}
function msort(inp) {
var mid = (inp.length / 2) << 0;
if(!mid) return inp;
return merge(
msort(inp.slice(0, mid)),
msort(inp.slice(mid)));
}
console.log(msort([6,3,5,0,2,4,1,7]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment