Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 44cf167

Browse files
sharansukesh1003pre-commit-ci[bot]MaximSmolskiy
authored
Create cyclic_sort.py (#9256)
* Create cyclic_sort.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update cyclic_sort.py * updating DIRECTORY.md * Update cyclic_sort.py * Update cyclic_sort.py * Update cyclic_sort.py * Update cyclic_sort.py * Update cyclic_sort.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru> Co-authored-by: MaximSmolskiy <MaximSmolskiy@users.noreply.github.com>
1 parent 55db5a1 commit 44cf167

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

‎DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@
12671267
* [Comb Sort](sorts/comb_sort.py)
12681268
* [Counting Sort](sorts/counting_sort.py)
12691269
* [Cycle Sort](sorts/cycle_sort.py)
1270+
* [Cyclic Sort](sorts/cyclic_sort.py)
12701271
* [Double Sort](sorts/double_sort.py)
12711272
* [Dutch National Flag Sort](sorts/dutch_national_flag_sort.py)
12721273
* [Exchange Sort](sorts/exchange_sort.py)

‎sorts/cyclic_sort.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
This is a pure Python implementation of the Cyclic Sort algorithm.
3+
4+
For doctests run following command:
5+
python -m doctest -v cyclic_sort.py
6+
or
7+
python3 -m doctest -v cyclic_sort.py
8+
For manual testing run:
9+
python cyclic_sort.py
10+
or
11+
python3 cyclic_sort.py
12+
"""
13+
14+
15+
def cyclic_sort(nums: list[int]) -> list[int]:
16+
"""
17+
Sorts the input list of n integers from 1 to n in-place
18+
using the Cyclic Sort algorithm.
19+
20+
:param nums: List of n integers from 1 to n to be sorted.
21+
:return: The same list sorted in ascending order.
22+
23+
Time complexity: O(n), where n is the number of integers in the list.
24+
25+
Examples:
26+
>>> cyclic_sort([])
27+
[]
28+
>>> cyclic_sort([3, 5, 2, 1, 4])
29+
[1, 2, 3, 4, 5]
30+
"""
31+
32+
# Perform cyclic sort
33+
index = 0
34+
while index < len(nums):
35+
# Calculate the correct index for the current element
36+
correct_index = nums[index] - 1
37+
# If the current element is not at its correct position,
38+
# swap it with the element at its correct index
39+
if index != correct_index:
40+
nums[index], nums[correct_index] = nums[correct_index], nums[index]
41+
else:
42+
# If the current element is already in its correct position,
43+
# move to the next element
44+
index += 1
45+
46+
return nums
47+
48+
49+
if __name__ == "__main__":
50+
import doctest
51+
52+
doctest.testmod()
53+
user_input = input("Enter numbers separated by a comma:\n").strip()
54+
unsorted = [int(item) for item in user_input.split(",")]
55+
print(*cyclic_sort(unsorted), sep=",")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /