Radix Sort. Θ(d(n+k)) => Θ(nu

2018-03-18  本文已影响7人  R0b1n_L33
from random import choices

def countingSort(source:[], digits:[], scope:int) -> []:
    counter = [0]*scope
    amount = len(source)

    for x in digits:
        counter[x] += 1

    for i in range(1, scope):
        counter[i] += counter[i-1]

    destination = [0]*amount

    for i in reversed(range(amount)):
        quantity = digits[i]
        # index = count-1
        destination[counter[quantity]-1] = source[i]
        counter[quantity] -= 1

    return destination

scope = 10
amount = 1000
numOfDigits = 3
source = choices(range(amount), k=amount)

for i in range(numOfDigits):
    digits = []
    for x in source:
        temp = x // (scope ** i)
        digit = temp % scope
        digits.append(digit)

    source = countingSort(source, digits, scope)
上一篇下一篇

猜你喜欢

热点阅读