li=[11,33,4,208,22,89,182,32,69,358,222] for j in range(1,len(li)):#为外层循环次数 for i in range(len(li)-j): current_value=li[i] next_value=li[i+1] if current_value > next_value: temp=li[i] li[i]=li[i+1] li[i+1]=temp print(li)
改进
li=[11,33,4,208,22,89,182,32,69,358,222]
for j in range(1,len(li)):#为外层循环次数
issorted=True
for i in range(len(li)-j):
current_value=li[i]
next_value=li[i+1]
if current_value > next_value:
temp=li[i]
li[i]=li[i+1]
li[i+1]=temp
issorted=False
if issorted:
break
print(li)