排序算法:(一)冒泡排序

2019-02-27  本文已影响3人  4v3r9
#bubble sort
#time complexity: O(n^2)
#space complexity: O(1)

a = [6,7,4,3,9,1,2]

def bubble(lst):
    print(lst)
    length = len(lst)
    if length <=1:
        return
    for i in range(0,length):
        for j in range(length-1,i,-1):
            if lst[j] < lst[j-1]:
                lst[j],lst[j-1] = lst[j-1],lst[j]
    print(lst)

bubble(a)
上一篇 下一篇

猜你喜欢

热点阅读