Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Last active August 29, 2015 14:08
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 shigemk2/53041ec2448e107b63df to your computer and use it in GitHub Desktop.
Save shigemk2/53041ec2448e107b63df to your computer and use it in GitHub Desktop.
挿入ソートを再帰で実装してみる 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