1、冒泡
data = [3,2,5,6,7,4,1] num = len(data) for i in range(num): for j in range(num-1): if data[j] > data[j+1]: #两个相邻的数做比较 data[j],data[j+1] = data[j+1],data[j] print(data) 控制台输出: [2, 3, 5, 6, 4, 1, 7] #执行一次循环后,最大数在最后 [2, 3, 5, 4, 1, 6, 7] [2, 3, 4, 1, 5, 6, 7] [2, 3, 1, 4, 5, 6, 7] [2, 1, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7] [1, 2, 3, 4, 5, 6, 7]
2、
3、