python冒泡法

b = [9,7,8,6,5,4,3,2,1]
count = 0
count_swap = 0
lenth = len(b)
for i in range(lenth):
  flag = False
  for j in range(lenth-i-1):
      count+=1
      if b[j] > b[j+1]:
          tem = b[j]
          b[j] = b[j+1]
          b[j+1] = tem
          flag = True#有交换过,状态改变
          count_swap+=1
  if not flag:#标记优化提升效率
      break
print(b,count_swap,count)

    原文作者:2e7f813af9b3
    原文地址: https://www.jianshu.com/p/0ae24830e04b
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞