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)

 

posted on 2018-03-07 14:58  sonofthesea  阅读(91)  评论(0编辑  收藏  举报