0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

選択ソート|備忘録

Posted at

大きい順にソーティングしてみた

よく使われる手法で、先頭から順に調べながら、
それまでの要素よりも大きい値が登場したらそのインデックスを記録する方法

select_sort1.py
data = [6,13,12,65,76,22,43,87,14,9]

for ii in range(len(data)):
    max = ii
    for jj in range( ii + 1, len(data)):
        if data[max] < data[jj]:
            max = jj

    # ここでSWAP
    data[ii], data[max] = data[max], data[ii]

print(data)

# 結果
# [87, 76, 65, 43, 22, 14, 13, 12, 9, 6]

この手法は容易に導入しやすい分、for二重ループとなるので、10件、20件程度なら大した差は出ないでしょうが、10万件、20万件といったソーティングでは負荷がかかりやすい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?