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)
时间: 2024-10-07 18:16:38