1 import random 2 def bubble_sort_1(li): 3 for i in range(len(li)-1): 4 exchange = False 5 for j in range(len(li)-i-1): 6 if li[j] > li[j+1]: 7 li[j],li[j+1] = li[j+1],li[j] 8 exchange = True 9 if not exchange: 10 return 11 12 li=list(range(1000)) 13 random.shuffle(li) #洗牌操作 14 bubble_sort_1(li) 15 print(li)