Python:排序(sort / 冒泡排序)

# coding:utf-8
'''
a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8]

问题1: 对列表 a 中的数字从小到大排序
问题2: 排序后去除重复的数字

'''


a = [1, 6, 8, 11, 9, 1, 8, 6, 8, 7, 8]


#方法一:sort排序


'''
1.sort 正序
a.sort()
print(a)

2.sort 倒叙
a.sort(reverse=True)
print(a)

3.去重
b = list(set(a))
print(b)
'''

b = sorted(a)
c = list(set(b))
print("列表a中的数字从小到大排序为: %s " %b)
print("排序后去除重复的数字 %s " %c)

print("-----------------------分割线-----------------------")


#方法二,冒泡排序

s = range(1,len(a))[::-1]

for i in s:
    for j in range(i):
        if a[j] > a[j+1]:
            a[j], a[j+1] = a[j+1], a[j]
    print("第 %s 轮交换后数据:%s" % (len(s)-i+1, a))

print("列表 a 中的数字从小到大排序为:%s"  %a)

y = list(set(a))
print("排序后去除重复的数字 %s " %y)

运行后的结果:

原文地址:https://www.cnblogs.com/JodieRao/p/12659225.html