Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Bug report for radix sort #336

Closed
@InnoFang

Description

@InnoFang

Description

if the test case for radix_sort.py is [104, 203, 308, 401], the result would be [401, 203, 104, 308]

It's wrong!

The reason is that if the tmp is always 0 in one loop, it will exit the loop. In other words, If the same digit of all numbers is 0, then the result may be wrong. The similar example like:
Input: [2018, 33017, 24016]
Output: [24016, 33017, 2018]
Wrong again!!

Suggestion

Do not use maxLength as a loop variable because the value of maxLength is related to tmp.

I think that by finding the maximum value of the array and assigning it to max_digit, using another variable digit with an initial value of 1 as the loop variable, each loop digit is multiplied by 10, and exit the loops when the digit greater than max_digit, which can guarantee the correct number of loops.

And the complexity will be O(nk + n) . n is the size of input list and k is the digit length of the number.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions