代码如下:
author = 'damao'
"""
冒泡排序:是一种计算机领域较简单的排序方法,他重复走访要排序的数列,
一次比较2个元素;如果顺序错误,就交换他们,一直重复走访直
到没有需要交换为止,也就是数列排序完成
"""
"""交换2个数"""
a = 10
b = 20
# 设一个临时变量c
# c = a
# a = b
# b = c
# print(a,b)
# 在python里面交换可以更方便
a, b = b, a
print(a)
print(b)
"""遍历比较相连得数"""
list_unb = [1,4,6,34,234,222,43,0,22]
for i in range(len(list_unb)-1): # 遍历整个列表
if list_unb[i] > list_unb[i+1]:
list_unb[i], list_unb[i+1] = list_unb[i+1],list_unb[i]
print(i)
print(list_unb)
print(list_unb)
print("========================")
"""冒泡排序"""
nub = [23,1,2,3,54,33,44,2,211]
new = range(len(nub))[::-1]
print(new)
for j in new:
for i in range(j):
if nub[i] > nub[i+1]:
nub[i], nub[i+1] = nub[i+1], nub[i]
print(nub)
"""sort()排序"""
a = [1,4,6,3,2,3,432432,54,65,76,57,6,76,767]
print(a) # 排序前
a.sort() # 可直接调用排序方法,what`s f uck!!!
print(a) # 排序后
缺点: 冒泡排序解决了桶排序浪费空间的问题, 但是冒泡排序的效率特别低