冒泡排序
实现思路: 使用双重for循环,内层变量为i, 外层为j,在内层循环中不断的比较相邻的两个值(i, i+1)的大小,如果i+1的值大于i的值,交换两者位置,每循环一次,外层的j增加1,等到j等于n-1的时候,结束循环
1 def bubbleSort(list): 2 n = len(list) 3 for j in range(0, n): 4 for i in range(0, n-j-1): 5 if list[i] > list[i+1]: 6 list[i], list[i+1] = list[i+1], list[i] 7 return list 8
9 if __name__ == '__main__': 10 list = [23, 12, 1, 56, 34, 78, 1, 55, 4, 2, 66] 11 print(bubbleSort(list))