1. 程式人生 > >Python八大排序之-----氣泡排序

Python八大排序之-----氣泡排序

def bubbleSort1():
    L = []
    listone = input("請輸入你要排序的玩意兒:")
    listone = listone.split(',')
    for v in listone:
        c = int(v)
        L.append(c)
    for i in range(len(L))[::-1]:
        for v in range(i):
            if L[v]>L[v+1]:
                L[v],L[v+1] = L[v+1],L[v]
    print(L)


def bubbleSort2():
    L = []
    listone = input("請輸入你要排序的玩意兒:")
    listone = listone.split(',')
    for v in listone:
        c = int(v)
        L.append(c)
    n = len(L)
    for j in range(0, n - 1):
            for i in range(0, n - 1 - j):
                if L[i] > L[i+1]:
                    L[i], L[i + 1] = L[i + 1],L[i]
    print(L)

bubbleSort1()
bubbleSort2()