2.python冒泡排序代码
代码如下:
def bubble(l):
flag = True
for i in range(len(l)-1, 0, -1):
if flag:
flag = False
for j in range(i):
if l[j] > l[j + 1]:
l[j], l[j+1] = l[j+1], l[j]
flag = True
else:
break
print l
li = [21,44,2,45,33,4,3,67]
bubble(li)
结果:[2, 3, 4, 21, 33, 44, 45, 67]