一、Python3实现冒泡排序
```python
def bubble_sort(nums):
for i in range(len(nums) - 1): # 这个循环负责设置冒泡排序进行的次数
for j in range(len(nums) - i - 1): # j为列表下标
if nums[j] > nums[j + 1]:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
return nums
print(bubble_sort([2, 1, 9, 3, 6, 7, 4, 8]))
#out:[1, 2, 3, 4, 6, 7, 8, 9]
二、Python2实现冒泡排序
def bubble(bubbleList):
listLength = len(bubbleList)
while listLength > 0:
for i in range(listLength - 1):
if bubbleList[i] > bubbleList[i+1]:
bubbleList[i], bubblelist[i+1] = bubblelist[i+1], bubblelist[i]
listLength -= 1
print bubbleList
if __name__ == '__main__':
bubbleList = [3, 4, 1, 2, 5, 8, 0]
bubble(bubbleList)
#out:[0, 1, 2, 3, 4, 5, 8]