mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-10 21:43:15 +08:00
refactor: cleanup CombSort
(#5303)
refactor: cleanup CombSort Co-authored-by: Alex Klymenko <alx@alx.com>
This commit is contained in:
@ -16,76 +16,57 @@ package com.thealgorithms.sorts;
|
|||||||
* @see SortAlgorithm
|
* @see SortAlgorithm
|
||||||
*/
|
*/
|
||||||
class CombSort implements SortAlgorithm {
|
class CombSort implements SortAlgorithm {
|
||||||
|
private static final double SHRINK_FACTOR = 1.3;
|
||||||
|
|
||||||
// To find gap between elements
|
/**
|
||||||
private int nextGap(int gap) {
|
* Method to find the next gap
|
||||||
// Shrink gap by Shrink factor
|
*
|
||||||
gap = (gap * 10) / 13;
|
* @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);
|
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
|
* @param arr the array to be sorted
|
||||||
* @return sorted array
|
* @param <T> the type of elements in the array
|
||||||
|
* @return the sorted array
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
public <T extends Comparable<T>> T[] sort(T[] arr) {
|
||||||
int size = arr.length;
|
int gap = arr.length;
|
||||||
|
|
||||||
// initialize gap
|
|
||||||
int gap = size;
|
|
||||||
|
|
||||||
// Initialize swapped as true to make sure that loop runs
|
|
||||||
boolean swapped = true;
|
boolean swapped = true;
|
||||||
|
|
||||||
// Keep running while gap is more than 1 and last iteration caused a swap
|
|
||||||
while (gap != 1 || swapped) {
|
while (gap != 1 || swapped) {
|
||||||
// Find next gap
|
gap = getNextGap(gap);
|
||||||
gap = nextGap(gap);
|
swapped = performSwaps(arr, 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver method
|
/**
|
||||||
public static void main(String[] args) {
|
* Method to perform the swapping of elements in the array based on the current gap
|
||||||
CombSort ob = new CombSort();
|
*
|
||||||
Integer[] arr = {
|
* @param arr the array to be sorted
|
||||||
8,
|
* @param gap the current gap
|
||||||
4,
|
* @param <T> the type of elements in the array
|
||||||
1,
|
* @return true if a swap occurred, false otherwise
|
||||||
56,
|
*/
|
||||||
3,
|
private <T extends Comparable<T>> boolean performSwaps(final T[] arr, final int gap) {
|
||||||
-44,
|
boolean swapped = false;
|
||||||
-1,
|
|
||||||
0,
|
|
||||||
36,
|
|
||||||
34,
|
|
||||||
8,
|
|
||||||
12,
|
|
||||||
-66,
|
|
||||||
-78,
|
|
||||||
23,
|
|
||||||
-6,
|
|
||||||
28,
|
|
||||||
0,
|
|
||||||
};
|
|
||||||
ob.sort(arr);
|
|
||||||
|
|
||||||
System.out.println("sorted array");
|
for (int i = 0; i < arr.length - gap; i++) {
|
||||||
SortUtils.print(arr);
|
if (SortUtils.less(arr[i + gap], arr[i])) {
|
||||||
|
SortUtils.swap(arr, i, i + gap);
|
||||||
|
swapped = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return swapped;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,66 +1,8 @@
|
|||||||
package com.thealgorithms.sorts;
|
package com.thealgorithms.sorts;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
public class CombSortTest extends SortingAlgorithmTest {
|
||||||
|
@Override
|
||||||
import org.junit.jupiter.api.Test;
|
SortAlgorithm getSortAlgorithm() {
|
||||||
|
return new CombSort();
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user