1 def bubble_sort2(alist): 2 for j in range(len(alist)-1,0,-1): 3 # j表示每次遍历需要比较的次数,是逐渐减小的 4 for i in range(j): 5 if alist[i] > alist[i+1]: 6 alist[i], alist[i+1] = alist[i+1], alist[i]
1 li = [54,26,93,17,77,31,44,55,20] 2 bubble_sort(li) 3 print(li) 4 5 6 7 output: 8 [17, 20, 26, 31, 44, 54, 55, 77, 93]
原文:https://www.cnblogs.com/GouQ/p/12783472.html