冒泡排序分析详细节情版
import random
def bubble(li):
for i in range(len(li)-1):
exchange = False
for j in range(len(li)-i-1):``
if li[j] > li[j+1]:
print('%s-%s哈哈已开始排序' % (i, j), li)
li[j], li[j+1] = li[j+1], li[j]
print('%s-%s哈哈已完成排序' % (i, j), li)
exchange = True
print('有变化',exchange)
print('%s-%s已完成一轮的排序'%(i,j), li)
if not exchange: #exchange是False not False = True
print('无变化',exchange)
return li
#
li = [9,1,2,4.5,4,5,6,7,8]
# li = list(range(10))
print('原始列表',li,len(li))
# bubble(li)
print('你好啊',bubble(li))
# li = [3,2,4,6,5,9,8,7,1]
# print(li)
# li = [random.randint(0,180) for i in range(50)]
# print('未开始排序',li)
# print('已完成排序',bubble(li))
# li = list(range(10000))
# random.shuffle(li)
# print('未开始排序',li)
# print('已完成排序',bubble(li))
自己写的简易版
#_*_ encoding: utf-8 _*_ @author: ty hery 2021/6/5
import time
def bubble_sort(li):
for i in range(len(li)-1):
exit = 0
for j in range(len(li)-i-1):
if li[j] > li[j+1]:
exit = 1
li[j+1],li[j] = li[j],li[j+1]
print('>>>',i+1)
if not exit:
# break
print('exit',i+1)
# continue
pass
print('第%s轮排序结果%s' % (i + 1, li))
li = [3,2,4,6,5,9,8,7,1,12,13,11,10]
print("原始是列表>>>%s"%li,len(li))
bubble_sort(li)
print(li)
标签:exchange,冒泡排序,li,range,详解,print,排序,bubble
From: https://www.cnblogs.com/heris/p/16827056.html