算法1:
def bubbleSort(numbers):
for j in range(0,len(numbers)):
for i in range(0,len(numbers)):
if numbers[j] < numbers[i]:
numbers[j],numbers[i] = numbers[i],numbers[j]
print(numbers)
def main():
numbers = [23,12,6]
bubbleSort(numbers)
if __name__ == '__main__':
main()
输出:
[3, 6, 1]
[6, 3, 1]
[6, 3, 1]
[3, 6, 1]
[3, 6, 1]
[3, 6, 1]
[1, 6, 3]
[1, 3, 6]
[1, 3, 6]
需要比较9次,才能得到结果
热门评论
[3,6,1]=>[3,16]=>[1,3,6]这才是冒泡
你这是选择排序不是冒泡排序吧