|
1 | 1 | package com.thealgorithms.sorts;
|
2 | 2 |
|
3 |
| -import static com.thealgorithms.sorts.SortUtils.less; |
4 |
| -import static com.thealgorithms.sorts.SortUtils.print; |
5 |
| - |
6 | 3 | /**
|
| 4 | + * This class implements the cycle sort algorithm. |
| 5 | + * Cycle sort is an in-place sorting algorithm, unstable, and efficient for scenarios with limited memory usage. |
7 | 6 | * @author Podshivalov Nikita (https://github.com/nikitap492)
|
8 | 7 | */
|
9 | 8 | class CycleSort implements SortAlgorithm {
|
10 |
| - |
| 9 | + /** |
| 10 | + * Sorts an array of comparable elements using the cycle sort algorithm. |
| 11 | + * |
| 12 | + * @param array the array to be sorted |
| 13 | + * @param <T> the type of elements in the array, must be comparable |
| 14 | + * @return the sorted array |
| 15 | + */ |
11 | 16 | @Override
|
12 |
| - public <T extends Comparable<T>> T[] sort(T[] arr) { |
13 |
| - int n = arr.length; |
14 |
| - |
15 |
| - // traverse array elements |
16 |
| - for (int j = 0; j <= n - 2; j++) { |
17 |
| - // initialize item as starting point |
18 |
| - T item = arr[j]; |
19 |
| - |
20 |
| - // Find position where we put the item. |
21 |
| - int pos = j; |
22 |
| - for (int i = j + 1; i < n; i++) { |
23 |
| - if (less(arr[i], item)) { |
| 17 | + public <T extends Comparable<T>> T[] sort(T[] array) { |
| 18 | + for (int cycleStart = 0; cycleStart <= array.length - 2; cycleStart++) { |
| 19 | + T item = array[cycleStart]; |
| 20 | + |
| 21 | + // Find the position where we put the element |
| 22 | + int pos = cycleStart; |
| 23 | + for (int i = cycleStart + 1; i < array.length; i++) { |
| 24 | + if (SortUtils.less(array[i], item)) { |
24 | 25 | pos++;
|
25 | 26 | }
|
26 | 27 | }
|
27 | 28 |
|
28 |
| - // If item is already in correct position |
29 |
| - if (pos == j) { |
| 29 | + // If the item is already in the correct position |
| 30 | + if (pos == cycleStart) { |
30 | 31 | continue;
|
31 | 32 | }
|
32 | 33 |
|
33 |
| - // ignore all duplicate elements |
34 |
| - while (item.compareTo(arr[pos]) == 0) { |
35 |
| - pos += 1; |
| 34 | + // Ignore all duplicate elements |
| 35 | + while (item.compareTo(array[pos]) == 0) { |
| 36 | + pos++; |
36 | 37 | }
|
37 | 38 |
|
38 |
| - // put the item to it's right position |
39 |
| - if (pos != j) { |
40 |
| - item = replace(arr, pos, item); |
| 39 | + // Put the item to its correct position |
| 40 | + if (pos != cycleStart) { |
| 41 | + item = replace(array, pos, item); |
41 | 42 | }
|
42 | 43 |
|
43 |
| - // Rotate rest of the cycle |
44 |
| - while (pos != j) { |
45 |
| - pos = j; |
| 44 | + // Rotate the rest of the cycle |
| 45 | + while (pos != cycleStart) { |
| 46 | + pos = cycleStart; |
46 | 47 |
|
47 |
| - // Find position where we put the element |
48 |
| - for (int i = j + 1; i < n; i++) { |
49 |
| - if (less(arr[i], item)) { |
50 |
| - pos += 1; |
| 48 | + // Find the position where we put the element |
| 49 | + for (int i = cycleStart + 1; i < array.length; i++) { |
| 50 | + if (SortUtils.less(array[i], item)) { |
| 51 | + pos++; |
51 | 52 | }
|
52 | 53 | }
|
53 | 54 |
|
54 |
| - // ignore all duplicate elements |
55 |
| - while (item.compareTo(arr[pos]) == 0) { |
56 |
| - pos += 1; |
| 55 | + // Ignore all duplicate elements |
| 56 | + while (item.compareTo(array[pos]) == 0) { |
| 57 | + pos++; |
57 | 58 | }
|
58 | 59 |
|
59 |
| - // put the item to it's right position |
60 |
| - if (item != arr[pos]) { |
61 |
| - item = replace(arr, pos, item); |
| 60 | + // Put the item to its correct position |
| 61 | + if (item != array[pos]) { |
| 62 | + item = replace(array, pos, item); |
62 | 63 | }
|
63 | 64 | }
|
64 | 65 | }
|
65 |
| - |
66 |
| - return arr; |
67 |
| - } |
68 |
| - |
69 |
| - private <T extends Comparable<T>> T replace(T[] arr, int pos, T item) { |
70 |
| - T temp = item; |
71 |
| - item = arr[pos]; |
72 |
| - arr[pos] = temp; |
73 |
| - return item; |
| 66 | + return array; |
74 | 67 | }
|
75 | 68 |
|
76 |
| - public static void main(String[] args) { |
77 |
| - Integer[] arr = { |
78 |
| - 4, |
79 |
| - 23, |
80 |
| - 6, |
81 |
| - 78, |
82 |
| - 1, |
83 |
| - 26, |
84 |
| - 11, |
85 |
| - 23, |
86 |
| - 0, |
87 |
| - -6, |
88 |
| - 3, |
89 |
| - 54, |
90 |
| - 231, |
91 |
| - 9, |
92 |
| - 12, |
93 |
| - }; |
94 |
| - CycleSort cycleSort = new CycleSort(); |
95 |
| - cycleSort.sort(arr); |
96 |
| - |
97 |
| - System.out.println("After sort : "); |
98 |
| - print(arr); |
| 69 | + /** |
| 70 | + * Replaces an element in the array with the given item and returns the replaced item. |
| 71 | + * |
| 72 | + * @param array the array in which the replacement will occur |
| 73 | + * @param pos the position at which the replacement will occur |
| 74 | + * @param item the item to be placed in the array |
| 75 | + * @param <T> the type of elements in the array, must be comparable |
| 76 | + * @return the replaced item |
| 77 | + */ |
| 78 | + private <T extends Comparable<T>> T replace(T[] array, int pos, T item) { |
| 79 | + T replacedItem = array[pos]; |
| 80 | + array[pos] = item; |
| 81 | + return replacedItem; |
99 | 82 | }
|
100 | 83 | }
|
0 commit comments