排序算法

2020-05-31  本文已影响0人  Loistein

# 冒泡排序-小数上浮,保证最小的在前面
def bubbleSortI(blist):
    length = len(blist)
    for i in range(length):
        for j in range(i+1,length):
            if blist[i] > blist[j]:
                blist[i],blist[j] = blist[j],blist[i]
    return blist

# 冒泡排序 - 大数沉底,保证最大的在最后面
def bubbleSortII(blist):
    length = len(blist)
    for i in range(length):
        for j in range(length-i-1):
            if blist[j] > blist[j+1]:
                blist[j],blist[j+1] = blist[j+1],blist[j]
    return blist

# 快速排序
def quickSort(blist):
    if blist == []:
        return []
    else:
        first = blist[0]
        less = quickSort([l for l in blist if l < first])
        more = quickSort([m for m in blist if m > first])
        return less + [first] + more

if __name__ == "__main__":
    print(bubbleSortI([8,6,9,1,3,10]))
    print(bubbleSortII([8, 6, 9, 1, 3, 10]))
    print(quickSort([8, 6, 9, 1, 3, 10]))
上一篇下一篇

猜你喜欢

热点阅读