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