与 Python 的排序相比,我的基数排序函数输出排序但错误的列表:
My radix sort: ['aa', 'a', 'ab', 'abs', 'asd', 'avc', 'axy', 'abid']
Python's sort: ['a', 'aa', 'ab', 'abid', 'abs', 'asd', 'avc', 'axy']
Run Code Online (Sandbox Code Playgroud)
* 我的基数排序不做填充
* 它的机制是最低有效位 (LSB)
* 我需要利用每个单词的长度
以下是我的代码。
def count_sort_letters(array, size, col, base):
output = [0] * size
count = [0] * base
min_base = ord('a')
for item in array:
correct_index = min(len(item) - 1, col)
letter = ord(item[-(correct_index + 1)]) - min_base
count[letter] += 1
for i in range(base - 1):
count[i + 1] += …Run Code Online (Sandbox Code Playgroud)