思想:数组中,相邻的元素根据索引顺序进行比较,每当逆序,则交换顺序,每次可以将最大值放在列表最后,并将其放在列表的最后,直到整个列表有序
def bubbleSort(ls):
for i in range(len(ls)):
for j in range(len(ls) - i-1):
if ls[j] > ls [j+1]:
ls[j], ls[j+1] = ls[j+1], ls[j]
return ls
a = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(bubbleSort(a))