对一组数由大到小进行排序 冒泡
a=[1,5,7.9,13,3,6]
print(len(a))
for i in range(len(a)-1):
print("i =",i)
for j in range(len(a)-1-i):
print("j=",j)
if a[j+1] > a[j]:
a[j],a[j+1]=a[j+1],a[j]
print(a)
print(a)
运行:
6
i = 0
j= 0
[5, 1, 7.9, 13, 3, 6]
j= 1
[5, 7.9, 1, 13, 3, 6]
j= 2
[5, 7.9, 13, 1, 3, 6]
j= 3
[5, 7.9, 13, 3, 1, 6]
j= 4
[5, 7.9, 13, 3, 6, 1]
i = 1
j= 0
[7.9, 5, 13, 3, 6, 1]
j= 1
[7.9, 13, 5, 3, 6, 1]
j= 2
j= 3
[7.9, 13, 5, 6, 3, 1]
i = 2
j= 0
[13, 7.9, 5, 6, 3, 1]
j= 1
j= 2
[13, 7.9, 6, 5, 3, 1]
i = 3
j= 0
j= 1
i = 4
j= 0
[13, 7.9, 6, 5, 3, 1]