From edb660afaebd119b4ef525b9060e6eb665c782b8 Mon Sep 17 00:00:00 2001 From: zhenxun00 <3085907783@qq.com> Date: Mon, 17 Aug 2026 19:05:34 +0800 Subject: [PATCH 1/2] fix: add input validation to cyclic_sort (#14898) ### Describe your change: Added input validation to cyclic_sort function to prevent infinite loops and silent errors when the input list contains duplicates or numbers outside the valid range. * [ ] Add an algorithm? * [x] Fix a bug or typo in an existing algorithm? * [ ] Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request. * [ ] Documentation change? ### Checklist: * [x] I have read CONTRIBUTING.md. * [x] This pull request is all my own work -- I have not plagiarized. * [x] I know that pull requests will not be merged if they fail the automated tests. * [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms. * [x] All new Python files are placed inside an existing directory. * [x] All filenames are in all lowercase characters with no spaces or dashes. * [x] All functions and variable names follow Python naming conventions. * [x] All function parameters and return values are annotated with Python type hints. * [x] All functions have doctests that pass the automated testing. * [ ] All new algorithms include at least one URL that points to Wikipedia or another similar explanation. * [x] If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: Fixes #14898 --- sorts/cyclic_sort.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9e81291548d4..f0aa5fb0b679 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -19,6 +19,8 @@ def cyclic_sort(nums: list[int]) -> list[int]: :param nums: List of n integers from 1 to n to be sorted. :return: The same list sorted in ascending order. + :raises ValueError: If input contains duplicate numbers. + :raises ValueError: If input contains numbers outside range 1 to n. Time complexity: O(n), where n is the number of integers in the list. @@ -27,7 +29,37 @@ def cyclic_sort(nums: list[int]) -> list[int]: [] >>> cyclic_sort([3, 5, 2, 1, 4]) [1, 2, 3, 4, 5] + >>> cyclic_sort([1]) + [1] + >>> cyclic_sort([2, 1]) + [1, 2] + + >>> cyclic_sort([7, 3, 2, 3, 54, 5, 4]) + Traceback (most recent call last): + ... + ValueError: All numbers must be unique, got [7, 3, 2, 3, 54, 5, 4] + + >>> cyclic_sort([1, 2, 5]) + Traceback (most recent call last): + ... + ValueError: All numbers must be in range 1 to 3, got 5 """ + n = len(nums) + + # Empty list is already sorted + if n == 0: + return nums + + # Check for duplicates + if len(set(nums)) != n: + raise ValueError(f"All numbers must be unique, got {nums}") + + # Check if all numbers are in range 1 to n + for num in nums: + if num < 1 or num > n: + raise ValueError( + f"All numbers must be in range 1 to {n}, got {num}" + ) # Perform cyclic sort index = 0 From 5f12ba103d07df98c6b8e7a9dd2e60fab4117613 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Aug 2026 18:08:38 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/cyclic_sort.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index f0aa5fb0b679..403e97cc50f8 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -57,9 +57,7 @@ def cyclic_sort(nums: list[int]) -> list[int]: # Check if all numbers are in range 1 to n for num in nums: if num < 1 or num > n: - raise ValueError( - f"All numbers must be in range 1 to {n}, got {num}" - ) + raise ValueError(f"All numbers must be in range 1 to {n}, got {num}") # Perform cyclic sort index = 0