冒泡排序的python实现
import random
import datetime
def bubble_sort(data) -> list:
"""
冒泡排序法的实现
:type data: list
:return:
"""
length = len(data)
start = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(start)
# 需要循环的次数
for item in range(length):
for j in range(length - 1 - item):
# 如果当前的数小于下一个数,则下一个数往前挪
if data[j] < data[j + 1]:
temp = data[j]
data[j] = data[j + 1]
data[j + 1] = temp
end = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(end)
return data
if __name__ == "__main__":
ls = list()
for i in range(1, 10000):
integer = random.choice(range(1, 2017))
ls.append(integer)
sort_result = bubble_sort(ls)
# print(sort_result)
排序一万个元素大概需要10 s