冒泡排序新方式

# Start with a list of numbers that ain't sorted
numbers=[0,5,1,4,2,8]
# Keep track of whether any swaps were made on the previous i teration
# If no swaps were made, the List is already sorted and we can stop
swapped = True
while swapped:
    # Set swapped to False SO we can check if any swaps are made Later
    swapped = False
    # For each pair of adjacent elements,
    for i in range(len(numbers) - 1):
        # If the first element is greater than the second,
        if numbers[i] > numbers[i + 1]:
        # Swap the el ements
            numbers[i], numbers[i + 1] = numbers[i + 1], numbers[i]
            # Set swapped to True SO we know to keep Looping
            swapped = True
# At this point, the list is sorted
print( numbers )
posted @   ty1539  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
历史上的今天:
2021-12-10 --go_out: protoc-gen-go: Plugin failed with status code 1.
2021-12-10 go 在windows编译出linux上运行的
点击右上角即可分享
微信分享提示