Last active
August 29, 2015 14:08
-
-
Save shigemk2/53041ec2448e107b63df to your computer and use it in GitHub Desktop.
挿入ソートを再帰で実装してみる ref: http://qiita.com/shigemk2/items/7cd4e1c19deb28aee101
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 insert(x, xs) { | |
if(xs.length <= 0) { | |
return [x]; | |
} | |
var y = xs.shift(); | |
if (x < y) { | |
return [x].concat([y].concat(xs)); | |
} else { | |
return [y].concat(insert(x, xs)); | |
} | |
} | |
function isort(xs) { | |
if (xs.length <= 0) return xs; | |
var x = xs.shift(); | |
return insert(x, isort(xs)); | |
} | |
console.log(isort([4,3,5,2,1])); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment