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 5033d88

Browse files
authored
Merge pull request #404 from KavinduDr/main
Create Cycle sort algorithm
2 parents a328b66 + 0afa310 commit 5033d88

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from typing import List
2+
3+
def cycle_sort(nums: List[int]) -> int:
4+
5+
writes = 0
6+
7+
for cycle_start in range(len(nums) - 1):
8+
current = nums[cycle_start]
9+
10+
# Find the target position for the current item.
11+
target_position = cycle_start
12+
for i in range(cycle_start + 1, len(nums)):
13+
if nums[i] < current:
14+
target_position += 1
15+
16+
# Skip if the item is already in the correct position.
17+
if target_position == cycle_start:
18+
continue
19+
20+
# Handle duplicates by finding the next available position.
21+
while current == nums[target_position]:
22+
target_position += 1
23+
24+
nums[target_position], current = current, nums[target_position]
25+
writes += 1
26+
27+
# Rotate the rest of the cycle.
28+
while target_position != cycle_start:
29+
target_position = cycle_start
30+
for i in range(cycle_start + 1, len(nums)):
31+
if nums[i] < current:
32+
target_position += 1
33+
34+
while current == nums[target_position]:
35+
target_position += 1
36+
37+
nums[target_position], current = current, nums[target_position]
38+
writes += 1
39+
40+
return writes
41+
42+
43+
if __name__ == "__main__":
44+
arr = [1, 8, 3, 9, 10, 10, 2, 4]
45+
print("Before sort:", arr)
46+
47+
writes = cycle_sort(arr)
48+
49+
print("After sort:", arr)
50+
print(f"Number of writes: {writes}")
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Cycle Sort Algorithm
2+
3+
## Overview
4+
Cycle Sort is a comparison-based sorting algorithm that is efficient when minimizing memory writes is important. It is an in-place sorting algorithm that rearranges the elements by identifying cycles in the permutation of elements.
5+
6+
## Algorithm Explanation
7+
The algorithm works by:
8+
1. Identifying the correct position of each element in the array.
9+
2. Placing the element in its correct position and replacing the element already there in the cycle.
10+
3. Repeating the process for the remaining unsorted elements.
11+
12+
## Complexity
13+
- **Time Complexity**:
14+
- Best, Worst, and Average Case: O(n2) (due to nested cycles).
15+
- **Space Complexity**: O(1) (in-place sorting).
16+
17+
## Usage Example
18+
```python
19+
from Cycle_Sort import cycle_sort
20+
21+
arr = [4, 5, 3, 2, 1]
22+
print("Original array:", arr)
23+
writes = cycle_sort(arr)
24+
print("Sorted array:", arr)
25+
print("Number of writes performed:", writes)

0 commit comments

Comments
(0)

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