Python 中的基数排序程序

问题描述 投票:0回答:3

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

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]

我正在尝试找出答案,但无法找出确切的问题。

python sorting radix-sort
3个回答
2
投票

这是一个很好的代码,但是您在缩进时犯了一个愚蠢的错误

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))

1
投票

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

# 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 number of iterations 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

0
投票

我有一个更简单的方法,假设列表中的最大数字是 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)
© www.soinside.com 2019 - 2024. All rights reserved.