美文网首页
py冒泡排序

py冒泡排序

作者: 事件_666 | 来源:发表于2019-05-27 13:12 被阅读0次

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

相关文章

网友评论

      本文标题:py冒泡排序

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