HackerRank Bubble Sort

x·2021년 5월 4일
0

problem-solving

목록 보기
12/18

정렬될 때까지 bubble sort를 하면서 몇 번이나 원소의 순서를 바꿨는지 출력

#!/bin/python3


def count_swaps(a):
    length = len(a)
    count = 0

    for _ in range(length):
        for j in range(length - 1):
            if a[j] > a[j + 1]:
                a[j], a[j + 1] = a[j + 1], a[j]
                count += 1

    print(f"Array is sorted in {count} swaps.")
    print(f"First Element: {a[0]}")
    print(f"Last Element: {a[-1]}")


if __name__ == "__main__":
    n = int(input())

    a = list(map(int, input().rstrip().split()))

    count_swaps(a=a)

0개의 댓글