Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

選択ソート

選択ソート(selection sort) は未ソート部分から最小値を選んで先頭と交換する操作を繰り返すソートアルゴリズム。バブルソートの改良版。

計算量
最悪時間計算量O(n2)O(n^2)
平均時間計算量O(n2)O(n^2)
最良時間計算量O(n2)O(n^2)
空間計算量O(1)O(1)(in-place)

特徴

  • 非安定ソート(同じ値の相対順序が崩れる場合がある)

  • in-place

  • 交換回数が最大でも n−1n-1 回と少ない(書き込みコストが高いメモリで有利)

アルゴリズム

  1. 未ソート部分 arr[i:] の最小値のインデックスを探す

  2. 最小値を arr[i] と交換する

  3. i を進めて繰り返す

入力: [64, 25, 12, 22, 11]
出力: [11, 12, 22, 25, 64]
ランダム20件: OK

ステップ可視化

入力: [5, 3, 8, 1, 4]
i=0: 最小=1 → [1, 3, 8, 5, 4]
i=1: 最小=3 → [1, 3, 8, 5, 4]
i=2: 最小=4 → [1, 3, 4, 5, 8]
i=3: 最小=5 → [1, 3, 4, 5, 8]
[1, 3, 4, 5, 8]