Radthon排序程序在python中

时间:2015-11-24 18:50:38

标签: python sorting radix-sort

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

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]

我试图解决它,但无法解决确切的问题。

3 个答案:

答案 0 :(得分:0)

这是一个很好的代码,但是在缩进xSpeed

时你犯了一个愚蠢的错误

当你使用placement *= RADIX移动到下一个数字时,它必须在for循环中执行

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

您可以将代码的一小部分更改为: -

placement *= RADIX

答案 1 :(得分:0)

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

a = [432,123,900,457,792,831,528,320,1,2,3,45,56,65,96,32,0,0,0,3,4,9,9,999] def radix_sort(a):     list1的= []     list2中= []     最后= []     plist = [[],[],[],[],[],[],[],[],[],[]]     plist1 = [[],[],[],[],[],[],[],[],[],[]]     plist2 = [[],[],[],[],[],[],[],[],[],[]]     对于范围内的s(len(a)):         x = a [s]%10         plist中[X] .append(一个[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)

答案 2 :(得分:0)

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

# 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