0
1

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.

pythonで挿入ソートを実装

Posted at

pythonでプログラミングの基礎練習1
*注意 pythonではL.sort()で並び替え可能ですが、アルゴリズム実装の練習として、コメントいただければ幸いです!

■ 挿入ソートの実装

N = int(input())
L = list(map(int,input().split()))

for i in range(N):
    for j in range(i):
        if L[i] < L[j]:
            ins = L[i]
            L[j+1:i+1] = L[j:i]
            L[j] = ins
            print(L)

■ input例

6
5 4 2 3 1 6

■ output例

[4, 5, 2, 3, 1, 6]
[2, 4, 5, 3, 1, 6]
[2, 3, 4, 5, 1, 6]
[1, 2, 3, 4, 5, 6]
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?