# encoding=utf-8 def bubble_sort(alist): """冒泡排序""" n = len(alist) for j in range(n-1): flag = True for i in range(n-1-j): if alist[i] > alist[i+1]: alist[i], alist[i+1] = alist[i+1], alist[i] flag = False # 没有交换,代表序列已经是有序的将时间复杂度降为了O(n) if flag: break if __name__ == ‘__main__‘: alist = [22,1,4,553,3,212,77] print(alist) bubble_sort(alist) print(alist)
时间: 2024-10-03 14:25:00