|
1 | | -def cycleSort(array): |
2 | | - writes = 0 # keeps track of the number of writes or swaps made during the sorting process |
3 | | - |
4 | | - # Loop through the array to find cycles to rotate. |
5 | | - for cycleStart in range(0, len(array) - 1): |
6 | | - item = array[cycleStart] |
7 | | - |
8 | | - # Find where to put the item. |
9 | | - position = cycleStart |
10 | | - for i in range(cycleStart + 1, len(array)): |
11 | | - if array[i] < item: |
12 | | - position += 1 |
13 | | - |
14 | | - # If the item is already there, this is not a cycle. |
15 | | - if position == cycleStart: |
16 | | - continue |
17 | | - |
18 | | - # Otherwise, put the item there or right after any duplicates. |
19 | | - while item == array[position]: |
20 | | - position += 1 |
21 | | - array[position], item = item, array[position] |
22 | | - writes += 1 |
23 | | - |
24 | | - # Rotate the rest of the cycle. |
25 | | - while position != cycleStart: |
26 | | - |
27 | | - # Find where to put the item. |
28 | | - position = cycleStart |
29 | | - for i in range(cycleStart + 1, len(array)): |
30 | | - if array[i] < item: |
31 | | - position += 1 |
32 | | - |
33 | | - # Put the item there or right after any duplicates. |
34 | | - while item == array[position]: |
35 | | - position += 1 |
36 | | - array[position], item = item, array[position] |
37 | | - writes += 1 |
38 | | - |
39 | | - return writes |
40 | | - |
41 | | - |
42 | | - |
43 | | -arr = [1, 8, 3, 9, 10, 10, 2, 4 ] |
44 | | -n = len(arr) |
45 | | -cycleSort(arr) |
46 | | - |
47 | | -print("After sort : ") |
48 | | -for i in range(0, n) : |
49 | | - print(arr[i], end = ' ') |
| 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}") |
0 commit comments