这是我根据描述写的代码:
def bubblesort(l):
for i,j in enumerate(l[:-1]):
for x,y in enumerate(l[i+1:]):
if j > y:
tem = l[i]
l[i] = l[x]
l[x] = tem
return l
这是豆瓣上找到的代码:
def bubble(x,n):
"""
冒泡排序,x是列表,n是列表长度
"""
for i in range(n):
for j in range(n-1):
if x[j]>x[j+1]:
t = x[j]
x[j] = x[j+1]
x[j+1] = t
return x
print bubble([1,10,2,5,41,25,3,48], 8) #[1, 2, 3, 5, 10, 25, 41, 48]
我感觉都一样啊。。。而且,我用enumerate是出于内存的考虑,不知道这想法对不对