代码很简单
def bubble_sort(l):
for i in xrange(0, len(l)-1) :
for n in xrange(0, len(l)) :
if l[i] > l[i+1] :
l[i], l[i+1] = l[i+1], l[i]
lst = [[1, 8, 2], [3, 2, 5], [2, 13, 3], [2, 5, 5], [2, 5, 6], [5, 11, 6], [5, 5, 6]]
print(lst)
bubble_sort(lst)
print(lst)
结果:
[[1, 8, 2], [3, 2, 5], [2, 13, 3], [2, 5, 5], [2, 5, 6], [5, 11, 6], [5, 5, 6]]
[[1, 8, 2], [2, 13, 3], [2, 5, 5], [2, 5, 6], [3, 2, 5], [5, 5, 6], [5, 11, 6]]
排序不正确。
为什么?