From 50bbd38ea7c33d657a75e51f97c1c4dc800559f8 Mon Sep 17 00:00:00 2001 From: Kavindu Dhananjaya Date: Fri, 10 Jan 2025 21:51:05 +0530 Subject: [PATCH 1/3] Create Cycle_Sort.py --- .../Sorting Algorithms/Cycle_Sort.py | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py diff --git a/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py b/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py new file mode 100644 index 00000000..834c0a33 --- /dev/null +++ b/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py @@ -0,0 +1,49 @@ +def cycleSort(array): + writes = 0 # keeps track of the number of writes or swaps made during the sorting process + + # Loop through the array to find cycles to rotate. + for cycleStart in range(0, len(array) - 1): + item = array[cycleStart] + + # Find where to put the item. + position = cycleStart + for i in range(cycleStart + 1, len(array)): + if array[i] < item: + position += 1 + + # If the item is already there, this is not a cycle. + if position == cycleStart: + continue + + # Otherwise, put the item there or right after any duplicates. + while item == array[position]: + position += 1 + array[position], item = item, array[position] + writes += 1 + + # Rotate the rest of the cycle. + while position != cycleStart: + + # Find where to put the item. + position = cycleStart + for i in range(cycleStart + 1, len(array)): + if array[i] < item: + position += 1 + + # Put the item there or right after any duplicates. + while item == array[position]: + position += 1 + array[position], item = item, array[position] + writes += 1 + + return writes + + + +arr = [1, 8, 3, 9, 10, 10, 2, 4 ] +n = len(arr) +cycleSort(arr) + +print("After sort : ") +for i in range(0, n) : + print(arr[i], end = ' ') From f2fc2d2b9fda26ce5958a5e41bf2e39e7f4a38d8 Mon Sep 17 00:00:00 2001 From: Kavindu Dhananjaya Date: Fri, 10 Jan 2025 22:08:24 +0530 Subject: [PATCH 2/3] Update Cycle_Sort.py --- .../Sorting Algorithms/Cycle_Sort.py | 99 ++++++++++--------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py b/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py index 834c0a33..1afd985f 100644 --- a/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py +++ b/Data Structures and Algorithms/Sorting Algorithms/Cycle_Sort.py @@ -1,49 +1,50 @@ -def cycleSort(array): - writes = 0 # keeps track of the number of writes or swaps made during the sorting process - - # Loop through the array to find cycles to rotate. - for cycleStart in range(0, len(array) - 1): - item = array[cycleStart] - - # Find where to put the item. - position = cycleStart - for i in range(cycleStart + 1, len(array)): - if array[i] < item: - position += 1 - - # If the item is already there, this is not a cycle. - if position == cycleStart: - continue - - # Otherwise, put the item there or right after any duplicates. - while item == array[position]: - position += 1 - array[position], item = item, array[position] - writes += 1 - - # Rotate the rest of the cycle. - while position != cycleStart: - - # Find where to put the item. - position = cycleStart - for i in range(cycleStart + 1, len(array)): - if array[i] < item: - position += 1 - - # Put the item there or right after any duplicates. - while item == array[position]: - position += 1 - array[position], item = item, array[position] - writes += 1 - - return writes - - - -arr = [1, 8, 3, 9, 10, 10, 2, 4 ] -n = len(arr) -cycleSort(arr) - -print("After sort : ") -for i in range(0, n) : - print(arr[i], end = ' ') +from typing import List + +def cycle_sort(nums: List[int]) -> int: + + writes = 0 + + for cycle_start in range(len(nums) - 1): + current = nums[cycle_start] + + # Find the target position for the current item. + target_position = cycle_start + for i in range(cycle_start + 1, len(nums)): + if nums[i] < current: + target_position += 1 + + # Skip if the item is already in the correct position. + if target_position == cycle_start: + continue + + # Handle duplicates by finding the next available position. + while current == nums[target_position]: + target_position += 1 + + nums[target_position], current = current, nums[target_position] + writes += 1 + + # Rotate the rest of the cycle. + while target_position != cycle_start: + target_position = cycle_start + for i in range(cycle_start + 1, len(nums)): + if nums[i] < current: + target_position += 1 + + while current == nums[target_position]: + target_position += 1 + + nums[target_position], current = current, nums[target_position] + writes += 1 + + return writes + + +if __name__ == "__main__": + arr = [1, 8, 3, 9, 10, 10, 2, 4] + print("Before sort:", arr) + + writes = cycle_sort(arr) + + print("After sort:", arr) + print(f"Number of writes: {writes}") From 0afa310347577d565e2b4398ba6b2e5dddc1f7b2 Mon Sep 17 00:00:00 2001 From: Kavindu Dhananjaya Date: Tue, 14 Jan 2025 15:54:18 +0530 Subject: [PATCH 3/3] Create README.md --- .../Sorting Algorithms/README.md | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Data Structures and Algorithms/Sorting Algorithms/README.md diff --git a/Data Structures and Algorithms/Sorting Algorithms/README.md b/Data Structures and Algorithms/Sorting Algorithms/README.md new file mode 100644 index 00000000..a951a505 --- /dev/null +++ b/Data Structures and Algorithms/Sorting Algorithms/README.md @@ -0,0 +1,25 @@ +# Cycle Sort Algorithm + +## Overview +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. + +## Algorithm Explanation +The algorithm works by: +1. Identifying the correct position of each element in the array. +2. Placing the element in its correct position and replacing the element already there in the cycle. +3. Repeating the process for the remaining unsorted elements. + +## Complexity +- **Time Complexity**: + - Best, Worst, and Average Case: O(n²) (due to nested cycles). +- **Space Complexity**: O(1) (in-place sorting). + +## Usage Example +```python +from Cycle_Sort import cycle_sort + +arr = [4, 5, 3, 2, 1] +print("Original array:", arr) +writes = cycle_sort(arr) +print("Sorted array:", arr) +print("Number of writes performed:", writes)