py冒泡排序

2019-05-27  本文已影响0人  事件_666

python 版本冒泡排序

def bubble_sort(list):
unsorted_until_index = len(list) - 1
sorted = False

while not sorted:
    sorted = True
    for i in range(unsorted_until_index):
        if list[i] > list[i+1]:
            sorted = False
            list[i], list[i+1] = list[i+1], list[i]
    unsorted_until_index = unsorted_until_index - 1

list = [65, 55, 45, 35, 25, 15, 10]
bubble_sort(list)
print list

上一篇下一篇

猜你喜欢

热点阅读