# 通过监测每趟比对是否发生过交换,可以提前确定排序是否完成
# 如果某趟比对没有发生任何交换,说明列表已经排好序,可以提前结束算法
def shortBubbleSort(alist):
exchanges = True
passnum = len(alist) - 1
while passnum > 0 and exchanges:
exchanges = False
for i in range(passnum):
if alist[i] > alist[i+1]:
exchanges = True
alist[i], alist[i+1] = alist[i+1], alist[i]
passnum -= 1
testlist = [2, 3, 3, 243, 24, 24455, 23]
shortBubbleSort(testlist)
print(testlist)