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?

def selection_sort(A, n):
    for i in range(0,n-1):
        # 最小値を探す
        for j in range(i+1,n):
            if A[j] < A[min_index] :
                min_index = j

        # A[i] と A[min_index]を交換
        A[i], A[min_index] = A[min_index],A[i]

        # A[0] ~ A[i] が整列済みになった
        print(*A)
        
        
n = int(input())
A = list(map(int,input().split()))
selection_sort(A,n)


1)最初のループでrangeがn-1,つまりn-2までになっているのは、最初の2個を比較するため。
2)1番目の数になっているiの要素と、それ以降、つまりi+1〜n-1(nからiをひいたもの)の中でループしている要素を比較し、最小のものを探す。
3)そしてA[i]とA[min_index]を交換する
4)この繰り返しで最終的にソートされる。

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?