美文网首页
排序算法:(一)冒泡排序

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

作者: 4v3r9 | 来源:发表于2019-02-27 08:01 被阅读3次
#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)

相关文章

网友评论

      本文标题:排序算法:(一)冒泡排序

      本文链接:https://www.haomeiwen.com/subject/ghztuqtx.html