#冒泡排序,升序
a = [4,5,1,6,3,7,1,10]
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
print(a)
[1, 1, 3, 4, 5, 6, 7, 10]
#冒泡排序,降序
a = [4,5,1,6,3,7,1,10]
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] < a[j]:
a[i], a[j] = a[j], a[i]
print(a)
[10, 7, 6, 5, 4, 3, 1, 1]
原文:http://blog.51cto.com/12013190/2115168