Skip to content

cyclic_sort.py has no input validation, causes infinite loop on invalid input #14898

Description

@dhruv-gautam-dev

Repository commit

b7e9f0b

Python version (python --version)

Python 3.12.3

Dependencies version (pip freeze)

customtkinter==6.0.0
darkdetect==0.8.0
et_xmlfile==2.0.0
greenlet==3.5.3
numpy==2.5.0
openpyxl==3.1.5
packaging==26.2
pandas==3.0.3
pillow==12.3.0
pip==24.0
playwright==1.61.0
pyee==13.0.1
pyperclip==1.11.0
python-dateutil==2.9.0.post0
python-dotenv==1.2.2
six==1.17.0
typing_extensions==4.16.0

Expected behavior

The function should raise a clear ValueError when:

  1. Input contains duplicate numbers
  2. Input contains numbers outside range 1 to n

Example:
cyclic_sort([7, 3, 2, 3, 54, 5, 4]) should raise:
ValueError: All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4]

cyclic_sort([1, 2, 5]) should raise:
ValueError: All numbers must be in range 1 to 3, got 5

Actual behavior

cyclic_sort([7, 3, 2, 3, 54, 5, 4]) → causes infinite loop,
program hangs and never returns

cyclic_sort([7, 5, 2, 4, 6, 3, 5, 4]) → returns wrong output
silently with no error

The function has no input validation at all. It only works correctly
for a list of exactly n integers from 1 to n with no duplicates,
but this constraint is never enforced or communicated to the user.

This causes learners who copy this implementation to get stuck in
infinite loops with no explanation of what went wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions