# 冒泡排序
import random
def Bubble_Sort(list):
for i in range(len(list)-1):
for j in range(len(list) - i - 1):
if list[j] >= list[j+1]:
b = list[j]
list[j] = list[j+1]
list[j+1] = b
return list
#python语法版
def Bubble_Sort_Python(list):
for i in range(len(list)-1):
for j in range(len(list) - i - 1):
if list[j] >= list[j+1]:
list[j],list[j+1] = list[j+1],list[j]
return list
# 顺序判断版
def Bubble_Sort_Python4(list):
for i in range(len(list)-1):
exchange = False
for j in range(len(list) - i - 1):
if list[j] >= list[j+1]:
list[j],list[j+1] = list[j+1],list[j]
exchange = True
print(list)
if not exchange:
return list
# return list
li = Bubble_Sort([8, 2, 3, 4, 5, 6, 6])
li2 = Bubble_Sort_Python([8, 2, 3, 4, 5, 6, 6])
li3 = Bubble_Sort_Python([random.randint(0, 10000) for i in range(1000)])
a = random.randint(0,10)
li4 = Bubble_Sort([7, 2, 3, 4, 5, 6])
print(Bubble_Sort_Python4(li4))
# print(li3)
# print(li)
# print(a)
排序算法【01冒泡】python
最新推荐文章于 2024-11-02 16:28:26 发布