LoginSignup
0
1

More than 3 years have passed since last update.

pythonでバブルソート

Last updated at Posted at 2021-03-29

pythonで実装練習2
今回も、pythonで実装練習です。
題材は、バブルソートですが、pythonでは、L.sort()で並び替えは可能です。

■ バブルソート

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

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

■ input

5
5 3 2 4 1

■output

[5, 3, 2, 1, 4]
[5, 3, 1, 2, 4]
[5, 1, 3, 2, 4]
[1, 5, 3, 2, 4]
[1, 5, 3, 2, 4]
[1, 5, 2, 3, 4]
[1, 2, 5, 3, 4]
[1, 2, 5, 3, 4]
[1, 2, 3, 5, 4]
[1, 2, 3, 4, 5]
0
1
2

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