Selection sort
平均計算時間: O(n^2)
不安定ソート
JavaScriptによる実装
function selectionSort(a) {
for (let i = 0; i < a.length - 1; i++) {
let minJ = i;
for (let j = i + 1; j < a.length; j++) {
if (a[j] < a[minJ]) {
minJ = j;
}
}
if (minJ != i) {
[a[i], a[minJ]] = [a[minJ], a[i]];
}
}
}
const a = [7, 15, 1, 8, 11, 5, 13, 0, 2, 4, 6, 3, 12, 14, 9, 10];
selectionSort(a);
console.log(a);
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]