@@ -9,74 +9,79 @@ class CycleSort implements SortAlgorithm {
9
9
/**
10
10
* Sorts an array of comparable elements using the cycle sort algorithm.
11
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
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
15
15
*/
16
16
@ 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 ;
20
19
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 );
28
23
29
- // If the item is already in the correct position
30
24
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
37
26
}
38
27
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 );
43
29
44
30
// Rotate the rest of the cycle
45
31
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 );
64
34
}
65
35
}
66
36
return array ;
67
37
}
68
38
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
+
69
74
/**
70
75
* Replaces an element in the array with the given item and returns the replaced item.
71
76
*
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
77
82
*/
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 ];
80
85
array [pos ] = item ;
81
86
return replacedItem ;
82
87
}
0 commit comments