冒泡排序 Bubble Sort (Python)

Python 3 实现:

def bubble_sort(nums):
    
    for i in range(len(nums) - 1, 0, -1):
        for j in range(i):
            if nums[j] > nums[j + 1]:
                tmp = nums[j + 1]
                nums[j + 1] = nums[j]
                nums[j] = tmp


if __name__ == '__main__':
    nums = [2, 1, 4, 5, 3, 7, 8, 6, 9, 0]
    bubble_sort(nums)
    print(nums)

源代码已上传至 Github,持续更新中。

    原文作者:e227f7966522
    原文地址: https://www.jianshu.com/p/2fe5f09f8661
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞