博主之前已经在理论上详解过各种排序方法,于是这里只放上自己用python实现的代码
基数排序示例代码如下:
#encoding:utf-8
import math
def radix_sort(lists,radix=10):
k = int(math.ceil(math.log(max(lists),radix)))
bucket = [[] for i in range(radix)]
for i in range(1,k+1):
for j in lists:
bucket[j/(radix**(i-1))%(radix**i)].append(j)
del lists[:]
for z in bucket:
lists = lists + z
del z[:]
return lists
if __name__ == "__main__":
lists = [3,4,2,8,9,5,1]
print("排序前序列为:"),
for i in lists:
print(i),
print("\n排序后结果为:"),
for i in (radix_sort(lists)):
print(i),