美文网首页
冒泡排序

冒泡排序

作者: 开始懂了90 | 来源:发表于2019-01-03 22:27 被阅读0次

初版

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

相关文章

网友评论

      本文标题:冒泡排序

      本文链接:https://www.haomeiwen.com/subject/djhcrqtx.html