Skip to content

Commit cc0ef53

Browse files
author
Alex Klymenko
committed
refactor: cleanup CycleSort. Adding test for it. Simplify code
1 parent ac31fba commit cc0ef53

File tree

2 files changed

+60
-69
lines changed

2 files changed

+60
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,83 @@
11
package com.thealgorithms.sorts;
22

3-
import static com.thealgorithms.sorts.SortUtils.less;
4-
import static com.thealgorithms.sorts.SortUtils.print;
5-
63
/**
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.
76
* @author Podshivalov Nikita (https://github.com/nikitap492)
87
*/
98
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+
*/
1116
@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)) {
2425
pos++;
2526
}
2627
}
2728

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) {
3031
continue;
3132
}
3233

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++;
3637
}
3738

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);
4142
}
4243

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;
4647

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++;
5152
}
5253
}
5354

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++;
5758
}
5859

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);
6263
}
6364
}
6465
}
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;
7467
}
7568

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;
9982
}
10083
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.thealgorithms.sorts;
2+
3+
public class CycleSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CycleSort();
7+
}
8+
}

0 commit comments

Comments
 (0)