Skip to content

Instantly share code, notes, and snippets.

@saranya-vatti
Last active October 8, 2015 17:56
Show Gist options
  • Save saranya-vatti/c16b8d007798c8f30022 to your computer and use it in GitHub Desktop.
Save saranya-vatti/c16b8d007798c8f30022 to your computer and use it in GitHub Desktop.
function merge(a, b) {
var c = new Array();
var k=0;
while(a.length || b.length) {
if(b.length == 0 || a[0]<=b[0]) {
c[k] = a.shift();
} else {
c[k] = b.shift();
}
k++;
}
return c;
}
function sort(arr) {
//base case:
if(arr.length <= 1) {
return arr;
} else {
return merge(sort(arr.splice(0,parseInt(arr.length/2))), sort(arr));
}
}
console.log("answer ...");
console.log(sort([6,4,9,2,8,1,10]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment