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 1 year has passed since last update.

Pythonで書く「アルゴリズムとデータ構造」 13_選択ソート

Last updated at Posted at 2023-07-01

コーディングテスト対策として、「アルゴリズムとデータ構造」という本を実践してます。
自分自身の備忘のために、pythonコードとメモを載せてます。

pythonコード

def selectionSort(A, N):
    swich_counter = 0
    for i in range(N):
        mini = i
        for j in range(i, N):
            if A[ j ] < A[ mini ]:
                mini = j
        if A[ i ] != A[ mini ]:
            A[ i ], A[ mini ] = A[ mini ], A[ i ]
            swich_counter = swich_counter + 1
    # いつものやつ(出力)
    print( ' '.join( map( str, A ) ) )
    print(swich_counter)

# いつものやつ(入力)
N_input = int( input() )
A_input = list( map( int, input().split(' ') ) )

selectionSort(A_input, N_input)

補足

if A[ i ] != A[ mini ]:

上記コードがないとswich_counterに正確に反映されません。

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?