python中的基数排序程序
Posted
技术标签:
【中文标题】python中的基数排序程序【英文标题】:Radix Sort program in python 【发布时间】:2015-11-24 18:50:38 【问题描述】:下面是一个通过基数排序方法进行排序的程序,但它并没有为每个输入显示正确的输出。
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 2 还是 3? 最明显的调试策略是在每次循环后打印出列表,和/或桶列表。 【参考方案1】:这是一个很好的代码,但是你在缩进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))
【讨论】:
placement *= RADIX
在那里什么都不做,因为该变量不再被使用。【参考方案2】:
我今天早上在玩,做了这个,它是一个递归基数函数。它适用于任意数量的数字,因此列表可以只是随机自然数。
# 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
【讨论】:
【参考方案3】:我有一个更简单的方法,假设列表中的最大数字是 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)
【讨论】:
以上是关于python中的基数排序程序的主要内容,如果未能解决你的问题,请参考以下文章