Added algorithm class CycleSort and its corresponding test class CycleSortTest
This commit is contained in:
parent
e15d329c71
commit
9763b6df89
87
src/main/java/src/main/java/com/sorts/CycleSort.java
Normal file
87
src/main/java/src/main/java/com/sorts/CycleSort.java
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
package src.main.java.com.sorts;
|
||||||
|
|
||||||
|
public class CycleSort {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param arr Array to be sorted
|
||||||
|
* @param <T> Generic type
|
||||||
|
* @return arr Sorted array
|
||||||
|
*/
|
||||||
|
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||||
|
|
||||||
|
int n = arr.length;
|
||||||
|
|
||||||
|
// Counter for the number of memory writes
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
// Traverse array and put the elements on their respective right places
|
||||||
|
for (int i = 0; i < n - 2; i++) {
|
||||||
|
|
||||||
|
// Initialize item as the starting point
|
||||||
|
T item = arr[i];
|
||||||
|
|
||||||
|
// Find the position where we want to put the item
|
||||||
|
// Basically we count all the smaller elements to the right of the item
|
||||||
|
int position = i;
|
||||||
|
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (arr[j].compareTo(item) < 0) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the element is already at the correct position...
|
||||||
|
if (position == i) {
|
||||||
|
|
||||||
|
// .. then we do not have to do anything
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore duplicate elements
|
||||||
|
while (item == arr[position]) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put the elements at its right position
|
||||||
|
if (position != i) {
|
||||||
|
|
||||||
|
// Swap
|
||||||
|
T temp = item;
|
||||||
|
item = arr[position];
|
||||||
|
arr[position] = temp;
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rotate remaining cycle
|
||||||
|
while (position != i) {
|
||||||
|
position = i;
|
||||||
|
|
||||||
|
// Find the position where we put the element
|
||||||
|
for (int j = i + 1; j < n; j++) {
|
||||||
|
if (arr[j].compareTo(item) < 0) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore duplicate elements
|
||||||
|
while (item == arr[position]) {
|
||||||
|
position++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put the element to its correct position
|
||||||
|
if (item != arr[position]) {
|
||||||
|
T temp = arr[position];
|
||||||
|
arr[position] = item;
|
||||||
|
item = temp;
|
||||||
|
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Number of memory writes :: " + count);
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
}
|
35
src/test/java/src/test/java/com/sorts/CycleSortTest.java
Normal file
35
src/test/java/src/test/java/com/sorts/CycleSortTest.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package src.test.java.com.sorts;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import src.main.java.com.sorts.CycleSort;
|
||||||
|
|
||||||
|
public class CycleSortTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void cycleSortIntegerTest() {
|
||||||
|
|
||||||
|
CycleSort cycleSort = new CycleSort();
|
||||||
|
|
||||||
|
// Test case for integers
|
||||||
|
Integer[] unsortedInt = new Integer[]{5, 1, 7, 0, 2, 9, 6, 3, 4, 8};
|
||||||
|
Integer[] sortedInt = new Integer[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
Assert.assertArrayEquals(sortedInt, cycleSort.sort(unsortedInt));
|
||||||
|
|
||||||
|
// Test case for floating point numbers
|
||||||
|
Float[] unsortedFloat = new Float[]{6.7f, 21.1f, 0.9f, -3.2f, 5.9f, -21.3f};
|
||||||
|
Float[] sortedFloat = new Float[]{-21.3f, -3.2f, 0.9f, 5.9f, 6.7f, 21.1f};
|
||||||
|
Assert.assertArrayEquals(sortedFloat, cycleSort.sort(unsortedFloat));
|
||||||
|
|
||||||
|
// Test case for characters
|
||||||
|
Character[] unsortedChar = new Character[]{'c', 'a', 'b', 'A', 'C', 'B'};
|
||||||
|
Character[] sortedChar = new Character[]{'A', 'B', 'C', 'a', 'b', 'c'};
|
||||||
|
Assert.assertArrayEquals(sortedChar, cycleSort.sort(unsortedChar));
|
||||||
|
|
||||||
|
// Test case for Strings
|
||||||
|
String[] unsortedStr = new String[]{"Edward", "Linus", "David", "Alan", "Dennis", "Robert", "Ken"};
|
||||||
|
String[] sortedStr = new String[]{"Alan", "David", "Dennis", "Edward", "Ken", "Linus", "Robert"};
|
||||||
|
Assert.assertArrayEquals(sortedStr, cycleSort.sort(unsortedStr));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user