嵌套循环,相邻的数字作比较,交换位置
# coding=utf-8
def bubble_sort(array):
for i in range(1, len(array)):
for j in range(0, len(array) - i):
if array[j] > array[j + 1]:
array[j], array[j + 1] = array[j + 1], array[j]
return array
if __name__ == '__main__':
array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
print(bubble_sort(array))