06 基数排序
import math def radixSort(list, radix = 10): ''' math.ceil 为x取整,结果是不小于x的最小整数. math.log(x, a) 返回 log 以 a 为底 x 的对数,若不给定 a 则底默认为 e ''' k = int(math.ceil(math.log(max(list), radix))) bucket = [[]for i in range(radix)] for i in range(1, k+1): for j in list: # math.floor(x)返回 ≦ x的最大整数,例:math.floor(3.4) = 3 bucket[math.floor(j / (radix **(i-1)) % (radix))].append(j) #print(bucket) # 从列表中删除所有元素 del list[:] for z in bucket: print (z) list +=z print (list) del z[:] return list list = [434,24,657,976,2354,9,67,8099,4353,3453] print(radixSort(list)) ''' import random import math def radixSort(): #A=[random.randint(1,9999) for i in range(10)] A =[5042,7431,3620,6306,858,8215,6438,2187,9830,2611] print(A) for k in range(4): #4轮排序 s=[[] for i in range(10)] for i in A: # 10**k 巧妙的把k=0问题解决了 s[math.floor(i/(10**k)%10)].append(i) # s = [[A,B,C],[]], b = [A,B,C],a=A. A=[a for b in s for a in b] return A print(radixSort()) '''