Last active
October 8, 2015 17:56
-
-
Save saranya-vatti/c16b8d007798c8f30022 to your computer and use it in GitHub Desktop.
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
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