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で書く「アルゴリズムとデータ構造」 11_挿入ソート

Last updated at Posted at 2023-07-01

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

pythonコード

#数値の個数
N_input = int( input() )

# N個の数値をリストに格納
A_input = list( map( int, input().split(' ') ) )

# 挿入ソート関数の定義
def insertionSort(A,N):
    for i in range(N):
        v = A[i]
        # 対象要素よりも左側にある要素
        j = i - 1
        # 大きな値を抽出する
        while (j >= 0 and v < A[j]):
            # 1つずつずらす
            A[ j+1 ] = A[ j ]
            j = j - 1
        A[ j+1 ] = v
        print(' '.join( map( str, A) ) )

insertionSort(A_input, N_input)

補足

入力・出力は決まった形があるので、コピペで対応。

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?