How does Radix Sort work?

前端 未结 4 1452
感动是毒
感动是毒 2021-01-30 08:48

I don\'t know why this is so hard for me to wrap my head around. I\'ve looked through the wiki pages, and pseudo code (as well as actual code) trying to understand how radix so

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 09:23

    may be my code could help you:)

    Here is python`s code of Radix Sort:

    import random,time
    
    from random import randint
    
    A=[random.randint(1,1212) for i in range(23)]
    
    
    length = len(str(max(A)))
    
    print(length) 
    
    rang = 10
    
    print(A)
    
    start=time.time()
    
    for i in range(length):
    
    B = [[] for k in range(rang)]
    
    for x in A:
    
        figure =x // (10**i) % 10
    
        B[figure].append(x)
    
    A = []
    
    for k in range(rang):
    
        A+=B[k]
    
    end=time.time()
    
    print(end-start)
    
    print(A)
    

提交回复
热议问题