Bubble sort in Pyton

Bubble sort

def bubble_sort(list_02):
    for j in range(len(list_02) - 1, 0, -1):
        count = 0
        for i in range(0, j):
            if list_02[i] > list_02[i + 1]:
                list_02[i], list_02[i + 1] = list_02[i + 1], list_02[i]
                count += 1
        if count == 0:
           return 

def main():
    list_01 = [2, 4, 55, 1, 6, 28, 54, 19, 95, 48, 65]
    bubble_sort(list_01)
    print(list_01)

if __name__ == '__main__':
    main()


---------------results of enforcement----------
[1, 2, 4, 6, 19, 28, 48, 54, 55, 65, 95]

-----------------------------------------------

猜你喜欢

转载自blog.csdn.net/Xcq007/article/details/82050673