diff --git a/src/main/java/com/sorts/CombSort.java b/src/main/java/com/sorts/CombSort.java new file mode 100644 index 000000000000..1f28c0f6d9fd --- /dev/null +++ b/src/main/java/com/sorts/CombSort.java @@ -0,0 +1,61 @@ +package com.sorts; + +import com.types.Sort; + +/** + * Comb Sort algorithm implementation + *

+ * Best-case performance O(n * log(n)) + * Worst-case performance O(n ^ 2) + * Worst-case space complexity O(1) + *

+ * Comb sort improves on bubble sort. + * + * @author Sandeep Roy (https://github.com/sandeeproy99) + * @author Podshivalov Nikita (https://github.com/nikitap492) + * + */ +class CombSort implements Sort { + + // To find gap between elements + private int nextGap(int gap) { + // Shrink gap by Shrink factor + gap = (gap * 10) / 13; + return (gap < 1) ? 1 : gap; + } + + /** + * Function to sort arr[] using Comb + * + * @param arr - an array should be sorted + * @return sorted array + */ + @Override + public > T[] sort(T arr[]) { + int size = arr.length; + + // initialize gap + int gap = size; + + // Initialize swapped as true to make sure that loop runs + boolean swapped = true; + + // Keep running while gap is more than 1 and last iteration caused a swap + while (gap != 1 || swapped) { + // Find next gap + gap = nextGap(gap); + + // Initialize swapped as false so that we can check if swap happened or not + swapped = false; + + // Compare all elements with current gap + for (int i = 0; i < size - gap; i++) { + if (SortUtils.less(arr[i + gap], arr[i])) { + // Swap arr[i] and arr[i+gap] + swapped = SortUtils.swap(arr, i, i + gap); + } + } + } + return arr; + } +} \ No newline at end of file diff --git a/src/test/java/com/sorts/CombSortTest.java b/src/test/java/com/sorts/CombSortTest.java new file mode 100644 index 000000000000..686e6a499840 --- /dev/null +++ b/src/test/java/com/sorts/CombSortTest.java @@ -0,0 +1,77 @@ +package com.sorts; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.*; + +class CombSortTest { + + CombSort combSort = new CombSort(); + + @Test + void testIntegerSort(){ + + Integer array1[] = { 9, 5, 4, 6, 0, 100, 91, 40, 140, 500, 4, -1, -19}; + Integer array2[] = { 9, 5, 4, 6, 0, 100, 91, 40, 140, 500, 4, -1, -19}; + + //using Java's tested sort algorithm to sort our unsorted array + Arrays.sort(array1); + + //sort our unsorted array using our new CombSort algorithm + combSort.sort(array2); + + //compare the two results, our CombSort vs. Java's sort + Assertions.assertArrayEquals(array1,array2); + } + + @Test + void testStringSort(){ + + String array1[] = { "Ten" , "Nine", "Eight", "Seven", "Six", "Five", "Four", "One", "Zero" }; + String array2[] = { "Ten" , "Nine", "Eight", "Seven", "Six", "Five", "Four", "One", "Zero" }; + + //using Java's tested sort algorithm to sort our unsorted array + Arrays.sort(array1); + + //sort our unsorted array using our new CombSort algorithm + combSort.sort(array2); + + //compare the two results, our CombSort vs. Java's sort + Assertions.assertArrayEquals(array1,array2); + } + + @Test + void testDoubleSort(){ + + Double array1[] = { 90.0, -10.0, 0.0, -1.0, -23.98, 10.76, 20.92 }; + Double array2[] = { 90.0, -10.0, 0.0, -1.0, -23.98, 10.76, 20.92 }; + + //using Java's tested sort algorithm to sort our unsorted array + Arrays.sort(array1); + + //sort our unsorted array using our new CombSort algorithm + combSort.sort(array2); + + //compare the two results, our CombSort vs. Java's sort + Assertions.assertArrayEquals(array1,array2); + } + + @Test + void testCharacterSort(){ + + Character array1[] = { 'Z', 'A', 'B', 'z', 'b', 'c' }; + Character array2[] = { 'Z', 'A', 'B', 'z', 'b', 'c' }; + + //using Java's tested sort algorithm to sort our unsorted array + Arrays.sort(array1); + + //sort our unsorted array using our new CombSort algorithm + combSort.sort(array2); + + //compare the two results, our CombSort vs. Java's sort + Assertions.assertArrayEquals(array1,array2); + } +} \ No newline at end of file