python 中的基数排序程序

Radix Sort program in python

下面是一个用于通过基数排序方法进行排序的程序,但它没有显示每个输入的正确输出。

def RadixSort(A):
    RADIX = 10
    maxLength = False
    tmp , placement = -1, 1

    while not maxLength:
        maxLength = True
        buckets = [list() for _ in range(RADIX)]
        for  i in A:
            tmp = i / placement
            buckets[tmp % RADIX].append(i)
            if maxLength and tmp > 0:
                maxLength = False

        a = 0
        for b in range(RADIX):
            buck = buckets[b]
            for i in buck:
                A[a] = i
                a += 1
        placement *= RADIX
    return A
A = []
n = input("Enter the numebr of elements you want to sort : ")
n = int(n)
print("Enter the numbers : \n")
for i in range(0, n):
    num = int(input())
    A.append(num)
print(RadixSort(A))

下面给出了几个输入/输出案例的例子:-

Enter the numebr of elements you want to sort : 5
Enter the numbers : 

5
4
3
2
1
[1, 2, 3, 4, 5]

这里我们得到了正确的输出,但在考虑下面的情况时,我们得到了错误的输出

Enter the numebr of elements you want to sort : 9
Enter the numbers : 

50
41
700
96
4
00
4
6
852
[50, 700, 0, 41, 852, 4, 4, 96, 6]

我正在尝试弄清楚,但无法获取确切的问题。

嗯,这是一个很好的代码,但是你在缩进时犯了一个愚蠢的错误 placement *= RADIX

因为你正在使用placement *= RADIX移动到下一个数字,所以它必须在for循环之外执行

所以,问题在于缩进 placement *= RADIX

您可以将您的代码稍微更改为:-

def RadixSort(A):
    RADIX = 10
    maxLength = False
    tmp , placement = -1, 1

    while not maxLength:
        maxLength = True
        buckets = [list() for _ in range(RADIX)]
        for  i in A:
            tmp = i // placement
            buckets[tmp % RADIX].append(i)
            if maxLength and tmp > 0:
                maxLength = False

        a = 0
        for b in range(RADIX):
            buck = buckets[b]
            for i in buck:
                A[a] = i
                a += 1
        # move to next digit
        placement *= RADIX
    return A
A = []
n = input("Enter the numebr of elements you want to sort : ")
n = int(n)
print("Enter the numbers : \n")
for i in range(0, n):
    num = int(input())
    A.append(num)
print(RadixSort(A))

我有一个更简单的方法,假设列表中的最大数字是 3 位数字

代码:

a = [ 432, 123, 900, 457, 792, 831, 528,320, 1 ,2 ,3 ,45, 56, 65, 96, 32, 0,0,0, 3 , 4, 900, 999 ]
def radix_sort(a):
    list1=[]
    list2=[]
    final=[]
    plist = [[],[],[],[],[],[],[],[],[],[]]
    plist1 = [[],[],[],[],[],[],[],[],[],[]]
    plist2 = [[],[],[],[],[],[],[],[],[],[]]
    for s in range(len(a)):
        x = a[s] % 10
        plist[x].append(a[s])

for i in range(len(plist)):
    for s in range(len(plist[i])):
        list1.append(plist[i][s])

for s in range(len(list1)):
    x=(list1[s] % 100) / 10
    plist1[x].append(list1[s])

for i in range(len(plist1)):
    for s in range(len(plist1[i])):
        list2.append(plist1[i][s])

for s in range(len(list2)):
    x=(list2[s] / 100) 
    plist2[x].append(list2[s])

for i in range(len(plist2)):
    for s in range(len(plist2[i])):
        final.append(plist2[i][s])
print ( " input list " , a)
print (" sorted list : " , final)

我今天早上玩弄了这个,这是一个递归基数函数。它适用于任意数量的数字,因此列表可以只是随机自然数。

# Get a digit from a number (right to left starting at 0)
def getDigit(number, digit):
    return number // 10**digit % 10

# Needed for the Count Sort inside the Radix
def prefixSum(toCalc=[]):
    for i in range (1, len(toCalc)):  
        toCalc[i] += toCalc[i-1]
    return toCalc

# Recursive radix sorting algorithm 
def radix(unsortedList, counter):
    # Allocate an empty list the same size as the unsorted list                    
    tempList = [None] * len(unsortedList) 
    # Used to store numbers from 0, 9
    counts = [0] * 10
    # for all the numbers, get the int[counter] and add to counts[]
    #   i.e. 231[1] = 3 
    for c in unsortedList:
        counts[getDigit(c, counter)] += 1
    prefixSum(counts)
    # Rearrange unsortedList to tempList
    for it in reversed(unsortedList):
        counts[getDigit(it, counter)] -= 1
        tempList[counts[getDigit(it, counter)]] = it

    # If the amount of itterations is < the largest digit length in the list
    # Run a pass on sorting the list again
    if counter < len(str(max(unsortedList))):
        # Recursion
        return radix(tempList, counter + 1)
    # Done sorting    
    else:            
        return unsortedList