From e9779a2e4546d7487d9b78ab66ab74ef03b23e11 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Sun, 1 Oct 2023 15:13:33 +0530 Subject: [PATCH 1/9] Create cyclic_sort.py --- sorts/cyclic_sort.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 sorts/cyclic_sort.py diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py new file mode 100644 index 000000000000..ac79956e4bcd --- /dev/null +++ b/sorts/cyclic_sort.py @@ -0,0 +1,53 @@ +""" +This is a pure Python implementation of the Cyclic Sort algorithm. + +For doctests run following command: +python -m doctest -v cyclic_sort.py +or +python3 -m doctest -v cyclic_sort.py +For manual testing run: +python cyclic_sort.py +""" + +def cyclic_sort(nums: list) -> list: + """ + Sorts the input list in-place using the Cyclic Sort algorithm. + + :param nums: List of integers to be sorted. + :return: The same list sorted in ascending order. + + Time complexity: O(n), where n is the number of elements in the list. + + Examples: + >>> cyclic_sort([3, 5, 2, 1, 4]) + [1, 2, 3, 4, 5] + >>> cyclic_sort([]) + [] + >>> cyclic_sort([-2, -5, -45]) + [-45, -5, -2] + """ + + # Perform cyclic sort + i = 0 + while i < len(nums): + # Calculate the correct index for the current element + correct_index = nums[i] - 1 + + # If the current element is not at its correct position, + # swap it with the element at its correct index + if nums[i] != nums[correct_index]: + nums[i], nums[correct_index] = nums[correct_index], nums[i] + else: + # If the current element is already in its correct position, + # move to the next element + i += 1 + + return nums + +if __name__ == "__main__": + import doctest + + doctest.testmod() + user_input = input("Enter numbers separated by a comma:\n").strip() + unsorted = [int(item) for item in user_input.split(",")] + print(*cyclic_sort(unsorted), sep=",") From 06fcd146bff803a742be7554038029c0ff75d61f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 1 Oct 2023 10:08:18 +0000 Subject: [PATCH 2/9] [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, 3 insertions(+), 1 deletion(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index ac79956e4bcd..afc59b1a2d7b 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -9,6 +9,7 @@ python cyclic_sort.py """ + def cyclic_sort(nums: list) -> list: """ Sorts the input list in-place using the Cyclic Sort algorithm. @@ -32,7 +33,7 @@ def cyclic_sort(nums: list) -> list: while i < len(nums): # Calculate the correct index for the current element correct_index = nums[i] - 1 - + # If the current element is not at its correct position, # swap it with the element at its correct index if nums[i] != nums[correct_index]: @@ -44,6 +45,7 @@ def cyclic_sort(nums: list) -> list: return nums + if __name__ == "__main__": import doctest From c1994c06e986ff52060c5b1e1d16379741216c17 Mon Sep 17 00:00:00 2001 From: Sharan Sukesh <77352136+sharansukesh1003@users.noreply.github.com> Date: Sun, 1 Oct 2023 23:43:51 +0530 Subject: [PATCH 3/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index afc59b1a2d7b..2deebc42a84b 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -24,8 +24,6 @@ def cyclic_sort(nums: list) -> list: [1, 2, 3, 4, 5] >>> cyclic_sort([]) [] - >>> cyclic_sort([-2, -5, -45]) - [-45, -5, -2] """ # Perform cyclic sort @@ -33,7 +31,6 @@ def cyclic_sort(nums: list) -> list: while i < len(nums): # Calculate the correct index for the current element correct_index = nums[i] - 1 - # If the current element is not at its correct position, # swap it with the element at its correct index if nums[i] != nums[correct_index]: From d89c2766277d02802246af8580629a576c171f07 Mon Sep 17 00:00:00 2001 From: MaximSmolskiy Date: Tue, 26 Aug 2025 18:21:18 +0000 Subject: [PATCH 4/9] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 2e3b4398f26e..53c53d208656 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -1267,6 +1267,7 @@ * [Comb Sort](sorts/comb_sort.py) * [Counting Sort](sorts/counting_sort.py) * [Cycle Sort](sorts/cycle_sort.py) + * [Cyclic Sort](sorts/cyclic_sort.py) * [Double Sort](sorts/double_sort.py) * [Dutch National Flag Sort](sorts/dutch_national_flag_sort.py) * [Exchange Sort](sorts/exchange_sort.py) From 4a02f78251d4d305a06a572e786963b5650c8e76 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 26 Aug 2025 21:27:00 +0300 Subject: [PATCH 5/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 2deebc42a84b..4d3bb421e1e9 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -7,10 +7,12 @@ python3 -m doctest -v cyclic_sort.py For manual testing run: python cyclic_sort.py +or +python3 cyclic_sort.py """ -def cyclic_sort(nums: list) -> list: +def cyclic_sort(nums: list[int]) -> list[int]: """ Sorts the input list in-place using the Cyclic Sort algorithm. @@ -20,25 +22,25 @@ def cyclic_sort(nums: list) -> list: Time complexity: O(n), where n is the number of elements in the list. Examples: - >>> cyclic_sort([3, 5, 2, 1, 4]) - [1, 2, 3, 4, 5] >>> cyclic_sort([]) [] + >>> cyclic_sort([3, 5, 2, 1, 4]) + [1, 2, 3, 4, 5] """ # Perform cyclic sort - i = 0 - while i < len(nums): + index = 0 + while index < len(nums): # Calculate the correct index for the current element - correct_index = nums[i] - 1 + correct_index = nums[index] - 1 # If the current element is not at its correct position, # swap it with the element at its correct index - if nums[i] != nums[correct_index]: - nums[i], nums[correct_index] = nums[correct_index], nums[i] + if index != correct_index: + nums[index], nums[correct_index] = nums[correct_index], nums[index] else: # If the current element is already in its correct position, # move to the next element - i += 1 + index += 1 return nums From 73a511af3fb5dd7d4475057db5afd81d812b6fd9 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 26 Aug 2025 21:28:47 +0300 Subject: [PATCH 6/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 4d3bb421e1e9..28c67f830068 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -29,18 +29,13 @@ def cyclic_sort(nums: list[int]) -> list[int]: """ # Perform cyclic sort - index = 0 - while index < len(nums): + for index in range(len(nums)): # Calculate the correct index for the current element correct_index = nums[index] - 1 # If the current element is not at its correct position, # swap it with the element at its correct index if index != correct_index: nums[index], nums[correct_index] = nums[correct_index], nums[index] - else: - # If the current element is already in its correct position, - # move to the next element - index += 1 return nums From 93c6183ec061427dee3601dc298fcc6768c8a7b8 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 26 Aug 2025 21:33:15 +0300 Subject: [PATCH 7/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 28c67f830068..4d3bb421e1e9 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -29,13 +29,18 @@ def cyclic_sort(nums: list[int]) -> list[int]: """ # Perform cyclic sort - for index in range(len(nums)): + index = 0 + while index < len(nums): # Calculate the correct index for the current element correct_index = nums[index] - 1 # If the current element is not at its correct position, # swap it with the element at its correct index if index != correct_index: nums[index], nums[correct_index] = nums[correct_index], nums[index] + else: + # If the current element is already in its correct position, + # move to the next element + index += 1 return nums From 43728a392cefe3922a504cf5f24feca16e871460 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 26 Aug 2025 21:37:56 +0300 Subject: [PATCH 8/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 4d3bb421e1e9..9cafb3b5e1e7 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -14,12 +14,12 @@ def cyclic_sort(nums: list[int]) -> list[int]: """ - Sorts the input list in-place using the Cyclic Sort algorithm. + Sorts the input list of n integers from 1 to n in-place using the Cyclic Sort algorithm. - :param nums: List of integers to be sorted. + :param nums: List of n integers from 1 to n to be sorted. :return: The same list sorted in ascending order. - Time complexity: O(n), where n is the number of elements in the list. + Time complexity: O(n), where n is the number of integers in the list. Examples: >>> cyclic_sort([]) From c1abeb4d35d0c490f348e78dce4f9a74ff00303c Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 26 Aug 2025 21:39:22 +0300 Subject: [PATCH 9/9] Update cyclic_sort.py --- sorts/cyclic_sort.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sorts/cyclic_sort.py b/sorts/cyclic_sort.py index 9cafb3b5e1e7..9e81291548d4 100644 --- a/sorts/cyclic_sort.py +++ b/sorts/cyclic_sort.py @@ -14,7 +14,8 @@ def cyclic_sort(nums: list[int]) -> list[int]: """ - Sorts the input list of n integers from 1 to n in-place using the Cyclic Sort algorithm. + Sorts the input list of n integers from 1 to n in-place + using the Cyclic Sort algorithm. :param nums: List of n integers from 1 to n to be sorted. :return: The same list sorted in ascending order.