From 9763b6df89d061dd7db3a5eee50eae50828f2b06 Mon Sep 17 00:00:00 2001 From: Anirudh Sharma Date: Wed, 2 Jan 2019 12:55:40 +0530 Subject: [PATCH] Added algorithm class CycleSort and its corresponding test class CycleSortTest --- .../src/main/java/com/sorts/CycleSort.java | 87 +++++++++++++++++++ .../test/java/com/sorts/CycleSortTest.java | 35 ++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/main/java/src/main/java/com/sorts/CycleSort.java create mode 100644 src/test/java/src/test/java/com/sorts/CycleSortTest.java diff --git a/src/main/java/src/main/java/com/sorts/CycleSort.java b/src/main/java/src/main/java/com/sorts/CycleSort.java new file mode 100644 index 00000000..5804a7b4 --- /dev/null +++ b/src/main/java/src/main/java/com/sorts/CycleSort.java @@ -0,0 +1,87 @@ +package src.main.java.com.sorts; + +public class CycleSort { + + /** + * @param arr Array to be sorted + * @param Generic type + * @return arr Sorted array + */ + public > 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; + } +} diff --git a/src/test/java/src/test/java/com/sorts/CycleSortTest.java b/src/test/java/src/test/java/com/sorts/CycleSortTest.java new file mode 100644 index 00000000..4e121255 --- /dev/null +++ b/src/test/java/src/test/java/com/sorts/CycleSortTest.java @@ -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)); + + } +}