Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active Aug 29, 2015
Embed
What would you like to do?
挿入ソートを再帰で実装してみる ref: http://qiita.com/shigemk2/items/7cd4e1c19deb28aee101
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