Skip to content

sorts/radix_sort.py: radix_sort() does not raise exception for negative inputs #14950

Description

@greemich-bot

Repository commit

567bb93

Python version (python --version)

Python 3.14.2

Dependencies version (pip freeze)

contourpy==1.3.3
cycler==0.12.1
dnspython==2.8.0
fonttools==4.62.1
iniconfig==2.3.0
kiwisolver==1.5.0
matplotlib==3.10.8
numpy==2.4.4
packaging==26.0
pillow==12.2.0
pluggy==1.6.0
Pygments==2.20.0
pymongo==4.17.0
pyparsing==3.3.2
pytest==9.1.1
pytest-timeout==2.4.0
python-dateutil==2.9.0.post0
pyzmq==27.1.0
six==1.17.0

Expected behavior

radix_sort() should raise a value error when a negative value is used.

ex. radix_sort([1, 3, -2, 4]) should raise ValueError because a negative value will not map to the correct bucket.

Actual behavior

When provided a negative value, radix_sort() proceeds with sorting and returns an incorrectly sorted array.
Ex. radix_sort([1, 3, -2, 4]) returns [1, 3, 4, -2]

Place test_radix_sort.py in the same directory and run "pytest test_radix_sort.py" to replicate.

Image

This is a small bug, but violates the definition that "Algorithms should: ... raise Python exceptions (ValueError, etc.) on erroneous input values."

Suggested action

Add a check when sorting the list_of_ints into buckets that raises a ValueError if i < 0. I am willing to implement this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions