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]