标签:5
1.冒泡算法:N个数从左到右,相邻两两比较,按照顺序排列。#冒泡排序,升序
a = [4,5,1,6,3,7,1,10]
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] > a[j]:
a[i], a[j] = a[j], a[i]
print(a)
[1, 1, 3, 4, 5, 6, 7, 10]
#冒泡排序,降序
a = [4,5,1,6,3,7,1,10]
for i in range(len(a)):
for j in range(i+1,len(a)):
if a[i] < a[j]:
a[i], a[j] = a[j], a[i]
print(a)
[10, 7, 6, 5, 4, 3, 1, 1]
标签:5
原文地址:http://blog.51cto.com/12013190/2115168