Skip to content

Instantly share code, notes, and snippets.

@fogus
Created November 13, 2008 20:33
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 fogus/24606 to your computer and use it in GitHub Desktop.
Save fogus/24606 to your computer and use it in GitHub Desktop.
/* merge sort in js */
function sort(a) {
var mid = a.length>>1;
if (mid==0) return a;
var less = sort(a.slice(0,mid));
var more = sort(a.slice(mid));
var merged = [];
do {
if (more[0] < less[0]) { var t=less; less=more; more=t; }
merged.push(less.shift());
} while (less.length > 0);
return merged.concat(more);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment