top = 1
i = 1
def bubble_sort(a):
global top
for i in range(len(a)-1):
j=i
for j in range (len(a)-i-1):
if a[j] > a[j+1]:
a[j], a[j+1] = a[j+1], a[j]
elif a[j] != a[j+1]:
a[j] = a[top]
top += 1
print(a)
s = ""
for i in range(top,len(a),1):
s = s + str(a[i])
print(s)
a = [4,8,7,8,5]
print(a)
bubble_sort(a)
没有报错,但是执行结果却很怪异??
[4, 8, 7, 8, 5]
[8, 8, 7, 8, 5]
[8, 7, 8, 8, 5]
[8, 7, 8, 8, 5]
[8, 7, 8, 5, 8]
858
[7, 8, 8, 5, 8]
[7, 8, 8, 5, 8]
[7, 8, 5, 8, 8]
588
[5, 8, 5, 8, 8]
[5, 5, 8, 8, 8]
88
[5, 5, 8, 8, 8]
88
本人初学python的菜鸟一枚。我想实现去重并升序。我知道有更好的方法,但我想知道按这个思路怎么改。谢谢!