小编Pen*_*now的帖子

Python中字符串的基数排序

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

python sorting radix-sort python-3.x counting-sort

2
推荐指数
1
解决办法
2892
查看次数

标签 统计

counting-sort ×1

python ×1

python-3.x ×1

radix-sort ×1

sorting ×1