首页 > 代码库 > Python写个Bubble排序

Python写个Bubble排序

 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 ===== ‘,j10                 if array[j] > array[j+1]:11                     array[j],array[j+1] = array[j+1],array[j]12                     flag = True13             #print array[j]14         else:15             break16     print array17 array=[21,44,2,45,33,4,3,67]18 bubble(array)
View Code

 

Python写个Bubble排序