Python中各种排序方法
本帖最后由 陶远航 于 2023-7-29 13:12 编辑Python中各种排序方法
import time
import random
from typing import List
#冒泡排序
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr > arr :
arr, arr = arr, arr
return arr
#快速排序
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr
left =
middle =
right =
return quick_sort(left) + middle + quick_sort(right)
#radix sort
def radix_sort(arr:List):
n = len(str(max(arr)))
for k in range(n):
bucket_list=[[] for i in range(10)]
for i in arr:
bucket_list.append(i)
arr=
return arr
#时间比较
list1=[]
for i in range(10000):
list1.append(random.randint(1,100000))
t1=time.time()
print(bubble_sort(list1))
t2=time.time()
print(t2-t1)
t1=time.time()
print(quick_sort(list1))
t2=time.time()
print(t2-t1)
t1=time.time()
print(radix_sort(list1))
t2=time.time()
print(t2-t1)
print(t2-t1)
评分{:10_256:}
二分查找:https://fishc.com.cn/thread-227636-1-1.html 币 币 顶 币 顶 顶 顶帖 顶{:7_146:} 渔币 {:10_257:} 336666 币 先收藏再琢磨,谢谢。 币
{:5_108:} 顶 哦 币
页:
[1]