Skip to content

Instantly share code, notes, and snippets.

@shoyan
Created September 4, 2013 09:09
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 shoyan/6434584 to your computer and use it in GitHub Desktop.
Save shoyan/6434584 to your computer and use it in GitHub Desktop.
単純挿入法で配列の値を昇順にソートする(JavaScript版)
/*
* 単純挿入法で配列の値を昇順にソートする
*/
var list = [8, 1, 3, 4, 9, 5],
i = 1,
j,
tmp;
for (i; i < list.length; i++) {
j = i;
while (j > 0) {
if (list[j - 1] < list[j]) {
break;
}
tmp = list[j - 1]
list[j - 1] = list[j]
list[j] = tmp
j -= 1;
}
}
console.log(list);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment