一, 排序思想
排序思想参见: https://www.cnblogs.com/luomeng/p/10161794.html
二, python 实现
- def bubble_sort(nums):
- """
- 冒泡排序
- Python3 range() 函数返回的是一个可迭代对象 (类型是对象), 而不是列表类型, 所以打印的时候不会打印列表.
- Python2 range() 函数返回的是列表.
- :type nums: 待排序列
- """
- for i in range(len(nums) - 1):
- for j in range(len(nums) - i - 1):
- if nums[j]> nums[j + 1]:
- # 交换两个变量的值,'='右边其实是一个元组 (Tuple)
- nums[j], nums[j + 1] = nums[j + 1], nums[j]
- nums = [4, 6, 2, 7, 1, 9, 0]
- bubble_sort(nums)
- print(nums)
来源: http://www.bubuko.com/infodetail-3010473.html