![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #-*-coding:utf-8-*- 2 def bubble(array): 3 flag = True; 4 for i in range(len(array)-1,0,-1):#从后往前比较 5 #print 'i = ',i 6 if flag: 7 flag = False 8 for j in range(i): 9 #print 'j ===== ',j 10 if array[j] > array[j+1]: 11 array[j],array[j+1] = array[j+1],array[j] 12 flag = True 13 #print array[j] 14 else: 15 break 16 print array 17 array=[21,44,2,45,33,4,3,67] 18 bubble(array)