Skip to content

Commit 9763b6d

Browse files
ani03shaAnirudh Sharma
authored and
Anirudh Sharma
committed
Added algorithm class CycleSort and its corresponding test class CycleSortTest
1 parent e15d329 commit 9763b6d

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package src.main.java.com.sorts;
2+
3+
public class CycleSort {
4+
5+
/**
6+
* @param arr Array to be sorted
7+
* @param <T> Generic type
8+
* @return arr Sorted array
9+
*/
10+
public <T extends Comparable<T>> T[] sort(T[] arr) {
11+
12+
int n = arr.length;
13+
14+
// Counter for the number of memory writes
15+
int count = 0;
16+
17+
// Traverse array and put the elements on their respective right places
18+
for (int i = 0; i < n - 2; i++) {
19+
20+
// Initialize item as the starting point
21+
T item = arr[i];
22+
23+
// Find the position where we want to put the item
24+
// Basically we count all the smaller elements to the right of the item
25+
int position = i;
26+
27+
for (int j = i + 1; j < n; j++) {
28+
if (arr[j].compareTo(item) < 0) {
29+
position++;
30+
}
31+
}
32+
33+
// Check if the element is already at the correct position...
34+
if (position == i) {
35+
36+
// .. then we do not have to do anything
37+
continue;
38+
}
39+
40+
// Ignore duplicate elements
41+
while (item == arr[position]) {
42+
position++;
43+
}
44+
45+
// Put the elements at its right position
46+
if (position != i) {
47+
48+
// Swap
49+
T temp = item;
50+
item = arr[position];
51+
arr[position] = temp;
52+
53+
count++;
54+
}
55+
56+
// Rotate remaining cycle
57+
while (position != i) {
58+
position = i;
59+
60+
// Find the position where we put the element
61+
for (int j = i + 1; j < n; j++) {
62+
if (arr[j].compareTo(item) < 0) {
63+
position++;
64+
}
65+
}
66+
67+
// Ignore duplicate elements
68+
while (item == arr[position]) {
69+
position++;
70+
}
71+
72+
// Put the element to its correct position
73+
if (item != arr[position]) {
74+
T temp = arr[position];
75+
arr[position] = item;
76+
item = temp;
77+
78+
count++;
79+
}
80+
}
81+
}
82+
83+
System.out.println("Number of memory writes :: " + count);
84+
85+
return arr;
86+
}
87+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package src.test.java.com.sorts;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import src.main.java.com.sorts.CycleSort;
6+
7+
public class CycleSortTest {
8+
9+
@Test
10+
public void cycleSortIntegerTest() {
11+
12+
CycleSort cycleSort = new CycleSort();
13+
14+
// Test case for integers
15+
Integer[] unsortedInt = new Integer[]{5, 1, 7, 0, 2, 9, 6, 3, 4, 8};
16+
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
17+
Assert.assertArrayEquals(sortedInt, cycleSort.sort(unsortedInt));
18+
19+
// Test case for floating point numbers
20+
Float[] unsortedFloat = new Float[]{6.7f, 21.1f, 0.9f, -3.2f, 5.9f, -21.3f};
21+
Float[] sortedFloat = new Float[]{-21.3f, -3.2f, 0.9f, 5.9f, 6.7f, 21.1f};
22+
Assert.assertArrayEquals(sortedFloat, cycleSort.sort(unsortedFloat));
23+
24+
// Test case for characters
25+
Character[] unsortedChar = new Character[]{'c', 'a', 'b', 'A', 'C', 'B'};
26+
Character[] sortedChar = new Character[]{'A', 'B', 'C', 'a', 'b', 'c'};
27+
Assert.assertArrayEquals(sortedChar, cycleSort.sort(unsortedChar));
28+
29+
// Test case for Strings
30+
String[] unsortedStr = new String[]{"Edward", "Linus", "David", "Alan", "Dennis", "Robert", "Ken"};
31+
String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"};
32+
Assert.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr));
33+
34+
}
35+
}

0 commit comments

Comments
 (0)