我只是写了另一个基数排序程序,这是我的代码:
#----------radix sort----------
def set_output():
output_list = []
for i in range (10):
output_list.append(queue())
return output_list
def set_radix(lists):
output_list = set_output()
for queue in lists:
for num in queue:
a = num[2]
output_list[int(a)].add(num)
return output_list
def sec_radix(input_list, i):
output_list = set_output()
for queue in input_list:
while len(queue) > 0:
num = queue.after()
a = num[i]
output_list[int(a)].add(num)
return output_list
def done_radix(num_list):
return sec_radix(sec_radix(set_radix(num_list), 1), 0)
python shell一直告诉我" IndexError:字符串索引超出范围",我的数字字符串是正确的。我认为我的def set_radix()
功能有问题,但我无法找到它的位置?
答案 0 :(得分:2)
这是一个基数排序编码,所以有人可以理解它。我从here偷了大部分内容。
def radixsort( aList ):
RADIX = 10
maxLength = False
tmp , placement = -1, 1
while not maxLength:
maxLength = True
# declare and initialize buckets
buckets = [list() for _ in range( RADIX )]
# split aList between lists
for i in aList:
tmp = i // placement
print ("i is " , i)
print ("placement is " , placement)
print ("tmp is ", tmp)
print ("tmp % RADIX is ", tmp % RADIX)
buckets[tmp % RADIX].append( i )
if maxLength and tmp > 0:
maxLength = False
# empty lists into aList array
a = 0
for b in range( RADIX ):
buck = buckets[b]
for i in buck:
aList[a] = i
a += 1
# move to next digit
placement *= RADIX
return aList
a = radixsort([18,5,100,3,1,19,6,0,7,4,2])
print(a)
我得到它的网站上的代码有一些问题来自%产生一个浮动。我怀疑从python 2到3的区别。