时间复杂度O(n^2)
1.laji版本
def bubble_sort(ls):
for i in range(len(ls)-1):
for j in range(len(ls)-i-1):
if ls[j]>ls[i]:
ls[j],ls[i]=ls[i],ls[j]
2.升级版
def bubble_sort(ls):
for i in range(len(ls)-1):
exchange=False
for j in range(len(ls)-i-1):
if ls[j]>ls[i]:
ls[j],ls[i]=ls[i],ls[j]
exchange=True
if not exchange:
return