1. 程式人生 > >python 氣泡排序

python 氣泡排序

from random import randint

def buddlesort(list):
    n = len(list)
    #總共排序n-1次
    for i in range(n-1):
        for j in range(n):
            if j + 1 <= n-1 and list[j] > list[j + 1]:
                tmp = list[j]
                list[j] = list[j+1]
                list[j+1] = tmp

    return list

list1 = []
for i in range(20):
    list1.append(randint(1,1000))

print(list1)
list1 = buddlesort(list1)
print(list1)