美文网首页
Radix Sort. Θ(d(n+k)) => Θ(nu

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

作者: R0b1n_L33 | 来源:发表于2018-03-18 00:02 被阅读7次
    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)
    

    相关文章

      网友评论

          本文标题:Radix Sort. Θ(d(n+k)) => Θ(nu

          本文链接:https://www.haomeiwen.com/subject/ejqjqftx.html