Skip to content

Commit 777de1d

Browse files
authored
refactor: cleanup CycleSort (#5321)
1 parent c4e0adb commit 777de1d

File tree

1 file changed

+54
-49
lines changed

1 file changed

+54
-49
lines changed

src/main/java/com/thealgorithms/sorts/CycleSort.java

+54-49
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,79 @@ class CycleSort implements SortAlgorithm {
99
/**
1010
* Sorts an array of comparable elements using the cycle sort algorithm.
1111
*
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
12+
* @param <T> The type of elements in the array, which must be comparable
13+
* @param array The array to be sorted
14+
* @return The sorted array
1515
*/
1616
@Override
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];
17+
public <T extends Comparable<T>> T[] sort(final T[] array) {
18+
final int length = array.length;
2019

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)) {
25-
pos++;
26-
}
27-
}
20+
for (int cycleStart = 0; cycleStart <= length - 2; cycleStart++) {
21+
T item = array[cycleStart];
22+
int pos = findPosition(array, cycleStart, item);
2823

29-
// If the item is already in the correct position
3024
if (pos == cycleStart) {
31-
continue;
32-
}
33-
34-
// Ignore all duplicate elements
35-
while (item.compareTo(array[pos]) == 0) {
36-
pos++;
25+
continue; // Item is already in the correct position
3726
}
3827

39-
// Put the item to its correct position
40-
if (pos != cycleStart) {
41-
item = replace(array, pos, item);
42-
}
28+
item = placeItem(array, item, pos);
4329

4430
// Rotate the rest of the cycle
4531
while (pos != cycleStart) {
46-
pos = cycleStart;
47-
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++;
52-
}
53-
}
54-
55-
// Ignore all duplicate elements
56-
while (item.compareTo(array[pos]) == 0) {
57-
pos++;
58-
}
59-
60-
// Put the item to its correct position
61-
if (item != array[pos]) {
62-
item = replace(array, pos, item);
63-
}
32+
pos = findPosition(array, cycleStart, item);
33+
item = placeItem(array, item, pos);
6434
}
6535
}
6636
return array;
6737
}
6838

39+
/**
40+
* Finds the correct position for the given item starting from cycleStart.
41+
*
42+
* @param <T> The type of elements in the array, which must be comparable
43+
* @param array The array to be sorted
44+
* @param cycleStart The starting index of the cycle
45+
* @param item The item whose position is to be found
46+
* @return The correct position of the item
47+
*/
48+
private <T extends Comparable<T>> int findPosition(final T[] array, final int cycleStart, final T item) {
49+
int pos = cycleStart;
50+
for (int i = cycleStart + 1; i < array.length; i++) {
51+
if (SortUtils.less(array[i], item)) {
52+
pos++;
53+
}
54+
}
55+
return pos;
56+
}
57+
58+
/**
59+
* Places the item in its correct position, handling duplicates, and returns the displaced item.
60+
*
61+
* @param <T> The type of elements in the array, which must be comparable
62+
* @param array The array being sorted
63+
* @param item The item to be placed
64+
* @param pos The position where the item is to be placed
65+
* @return The displaced item
66+
*/
67+
private <T extends Comparable<T>> T placeItem(final T[] array, final T item, int pos) {
68+
while (item.compareTo(array[pos]) == 0) {
69+
pos++;
70+
}
71+
return replace(array, pos, item);
72+
}
73+
6974
/**
7075
* Replaces an element in the array with the given item and returns the replaced item.
7176
*
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+
* @param <T> The type of elements in the array, which must be comparable
78+
* @param array The array in which the replacement will occur
79+
* @param pos The position at which the replacement will occur
80+
* @param item The item to be placed in the array
81+
* @return The replaced item
7782
*/
78-
private <T extends Comparable<T>> T replace(T[] array, int pos, T item) {
79-
T replacedItem = array[pos];
83+
private <T extends Comparable<T>> T replace(final T[] array, final int pos, final T item) {
84+
final T replacedItem = array[pos];
8085
array[pos] = item;
8186
return replacedItem;
8287
}

0 commit comments

Comments
 (0)