# 冒泡排序python实现 def bubble_sort(alist): count = 0 # 外层循环控制比较几轮 n = len(alist) for j in range(n - 1): # 内存循环控制交换 # -j是不再换已经排好的 for i in range(n - 1 - j): # 若前一个比后一个大,则换 if alist[i] > alist[i + 1]: alist[i], alist[i + 1] = alist[i + 1], alist[i] count = count + 1 print("第" + str(count) + "次:" + str(alist)) if __name__ == '__main__': li = [33, 11, 26, 7632, 2, 0, 78, 3, 9, 40, 222, -9, 62] print(li) bubble_sort(li) print(li)