Sorting: Bubble Sort

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the countSwaps function below.
def countSwaps(a):
    count=0
    for i in range(n):
        for j in range(n-1-i):####
            if a[j]>a[j+1]:
                a[j],a[j+1]=a[j+1],a[j]
                count+=1
    
    print('Array is sorted in '+str(count)+' swaps.')
    print('First Element: '+str(a[0]))
    print('Last Element: '+str(a[-1]))
    

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

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

    countSwaps(a)
发布了163 篇原创文章 · 获赞 90 · 访问量 6286

猜你喜欢

转载自blog.csdn.net/weixin_45405128/article/details/104217696