Skip to content

Instantly share code, notes, and snippets.

@timakin
Created February 19, 2015 14:17
Show Gist options
  • Save timakin/fc837b7f913ac95d1da5 to your computer and use it in GitHub Desktop.
Save timakin/fc837b7f913ac95d1da5 to your computer and use it in GitHub Desktop.
文系が学ぶコンピューターサイエンス╭( ・ㅂ・)و ̑̑:第5回【選択ソート】 ref: http://qiita.com/timakin/items/29b69034a6747f7b5521
// 選択ソート。計算量O(n^2)。
selectionSort: function(data) {
var min;
for (var i = 0; i < data.length - 1; i++) {
// 先頭を最小値とする。
min = i;
// 先頭から順に、それより小さい値があった場合そのうち一番小さいものと交換する。
//
for (var j = i + 1; j < data.length; j++) {
if (data[min] > data[j]) { min = j };
}
if (i != min) { this.swap(data, i, min) };
}
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment