Skip to content

Commit 8a32cba

Browse files
authored
Merge pull request #1284 from vedantzope9/main
Added Cyclic Sort algorithm
2 parents 2982d6d + afec460 commit 8a32cba

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

contrib/ds-algorithms/sorting-algorithms.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,3 +561,58 @@ print("Sorted string:", sorted_str)
561561
### Complexity Analysis
562562
- **Time Complexity:** O(n+k) for all cases.No matter how the elements are placed in the array, the algorithm goes through n+k times
563563
- **Space Complexity:** O(max). Larger the range of elements, larger is the space complexity.
564+
565+
566+
## 9. Cyclic Sort
567+
568+
### Theory
569+
Cyclic Sort is an in-place sorting algorithm that is useful for sorting arrays where the elements are in a known range (e.g., 1 to N). The key idea behind the algorithm is that each number should be placed at its correct index. If we find a number that is not at its correct index, we swap it with the number at its correct index. This process is repeated until every number is at its correct index.
570+
571+
### Algorithm
572+
- Iterate over the array from the start to the end.
573+
- For each element, check if it is at its correct index.
574+
- If it is not at its correct index, swap it with the element at its correct index.
575+
- Continue this process until the element at the current index is in its correct position. Move to the next index and repeat the process until the end of the array is reached.
576+
577+
### Steps
578+
- Start with the first element.
579+
- Check if it is at the correct index (i.e., if arr[i] == i + 1).
580+
- If it is not, swap it with the element at the index arr[i] - 1.
581+
- Repeat step 2 for the current element until it is at the correct index.
582+
- Move to the next element and repeat the process.
583+
584+
### Code
585+
586+
```python
587+
def cyclic_sort(nums):
588+
i = 0
589+
while i < len(nums):
590+
correct_index = nums[i] - 1
591+
if nums[i] != nums[correct_index]:
592+
nums[i], nums[correct_index] = nums[correct_index], nums[i] # Swap
593+
else:
594+
i += 1
595+
return nums
596+
```
597+
598+
### Example
599+
```
600+
arr = [3, 1, 5, 4, 2]
601+
sorted_arr = cyclic_sort(arr)
602+
print(sorted_arr)
603+
```
604+
### Output
605+
```
606+
[1, 2, 3, 4, 5]
607+
```
608+
609+
### Complexity Analysis
610+
**Time Complexity:**
611+
612+
The time complexity of Cyclic Sort is **O(n)**.
613+
This is because in each cycle, each element is either placed in its correct position or a swap is made. Since each element is swapped at most once, the total number of swaps (and hence the total number of operations) is linear in the number of elements.
614+
615+
**Space Complexity:**
616+
617+
The space complexity of Cyclic Sort is **O(1)**.
618+
This is because the algorithm only requires a constant amount of additional space beyond the input array.

0 commit comments

Comments
 (0)