冒泡排序

2019-01-03  本文已影响0人  开始懂了90

初版

li = [9,3,4,1,6,8,5,13,11]
index = 0

for j in range(len(li),1,-1):
    print j
    index = 0
    for i in range(1,j):  
        high = index + 1
        print i,index,high
        if li[index] > li[high]:
            pa = li[index]
            li[index] = li[high]
            li[high] = pa
            index += 1
            
        else:
            index += 1
print li

终版

li = [9,3,4,1,6,8,5,13,11]

for j in range(len(li)-1):
    for i in range(len(li)-1-j):
        if li[i] > li[i+1]:
            li[i],li[i+1] = li[i+1],li[i]
print li
上一篇下一篇

猜你喜欢

热点阅读