diff --git a/src/main/java/com/thealgorithms/sorts/CombSort.java b/src/main/java/com/thealgorithms/sorts/CombSort.java index edf09a2eb..cd12a5b28 100644 --- a/src/main/java/com/thealgorithms/sorts/CombSort.java +++ b/src/main/java/com/thealgorithms/sorts/CombSort.java @@ -16,76 +16,57 @@ package com.thealgorithms.sorts; * @see SortAlgorithm */ class CombSort implements SortAlgorithm { + private static final double SHRINK_FACTOR = 1.3; - // To find gap between elements - private int nextGap(int gap) { - // Shrink gap by Shrink factor - gap = (gap * 10) / 13; + /** + * Method to find the next gap + * + * @param gap the current gap + * @return the next gap value + */ + private int getNextGap(int gap) { + gap = (int) (gap / SHRINK_FACTOR); return Math.max(gap, 1); } /** - * Function to sort arr[] using Comb + * Method to sort the array using CombSort * - * @param arr - an array should be sorted - * @return sorted array + * @param arr the array to be sorted + * @param the type of elements in the array + * @return the 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 + int gap = arr.length; 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] - SortUtils.swap(arr, i, i + gap); - swapped = true; - } - } + gap = getNextGap(gap); + swapped = performSwaps(arr, gap); } + return arr; } - // Driver method - public static void main(String[] args) { - CombSort ob = new CombSort(); - Integer[] arr = { - 8, - 4, - 1, - 56, - 3, - -44, - -1, - 0, - 36, - 34, - 8, - 12, - -66, - -78, - 23, - -6, - 28, - 0, - }; - ob.sort(arr); + /** + * Method to perform the swapping of elements in the array based on the current gap + * + * @param arr the array to be sorted + * @param gap the current gap + * @param the type of elements in the array + * @return true if a swap occurred, false otherwise + */ + private > boolean performSwaps(final T[] arr, final int gap) { + boolean swapped = false; - System.out.println("sorted array"); - SortUtils.print(arr); + for (int i = 0; i < arr.length - gap; i++) { + if (SortUtils.less(arr[i + gap], arr[i])) { + SortUtils.swap(arr, i, i + gap); + swapped = true; + } + } + + return swapped; } } diff --git a/src/test/java/com/thealgorithms/sorts/CombSortTest.java b/src/test/java/com/thealgorithms/sorts/CombSortTest.java index e33fc388c..6b70ffacd 100644 --- a/src/test/java/com/thealgorithms/sorts/CombSortTest.java +++ b/src/test/java/com/thealgorithms/sorts/CombSortTest.java @@ -1,66 +1,8 @@ package com.thealgorithms.sorts; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import org.junit.jupiter.api.Test; - -/** - * @author Tabbygray (https://github.com/Tabbygray) - * @see CombSort - */ - -public class CombSortTest { - - private CombSort combSort = new CombSort(); - - @Test - public void combSortEmptyArray() { - Integer[] inputArray = {}; - Integer[] outputArray = combSort.sort(inputArray); - Integer[] expectedOutput = {}; - assertArrayEquals(outputArray, expectedOutput); - } - - @Test - public void combSortSingleStringElement() { - String[] inputArray = {"Test"}; - String[] outputArray = combSort.sort(inputArray); - String[] expectedArray = {"Test"}; - assertArrayEquals(outputArray, expectedArray); - } - - @Test - public void combSortStringArray() { - String[] inputArray = {"4gp8", "aBJ2", "85cW", "Pmk9", "ewZO", "meuU", "RhNd", "5TKB", "eDd5", "zzyo"}; - String[] outputArray = combSort.sort(inputArray); - String[] expectedArray = {"4gp8", "5TKB", "85cW", "Pmk9", "RhNd", "aBJ2", "eDd5", "ewZO", "meuU", "zzyo"}; - assertArrayEquals(outputArray, expectedArray); - } - - @Test - public void combSortIntegerArray() { - Integer[] inputArray = {36, 98, -51, -23, 66, -58, 31, 25, -30, 40}; - Integer[] outputArray = combSort.sort(inputArray); - Integer[] expectedArray = {-58, -51, -30, -23, 25, 31, 36, 40, 66, 98}; - assertArrayEquals(outputArray, expectedArray); - } - - @Test - public void combSortDoubleArray() { - Double[] inputArray = {0.8335545399, 0.9346214114, 0.3096396752, 0.6433840668, 0.3973191975, 0.6118850724, 0.0553975453, 0.1961108601, 0.6172800885, 0.1065247772}; - Double[] outputArray = combSort.sort(inputArray); - Double[] expectedArray = { - 0.0553975453, - 0.1065247772, - 0.1961108601, - 0.3096396752, - 0.3973191975, - 0.6118850724, - 0.6172800885, - 0.6433840668, - 0.8335545399, - 0.9346214114, - }; - assertArrayEquals(outputArray, expectedArray); +public class CombSortTest extends SortingAlgorithmTest { + @Override + SortAlgorithm getSortAlgorithm() { + return new CombSort(); } }