Skip to content

Commit fccd141

Browse files
alxkmAlex Klymenko
and
Alex Klymenko
authored
refactor: cleanup CombSort (#5303)
refactor: cleanup CombSort Co-authored-by: Alex Klymenko <[email protected]>
1 parent 5113101 commit fccd141

File tree

2 files changed

+39
-116
lines changed

2 files changed

+39
-116
lines changed

src/main/java/com/thealgorithms/sorts/CombSort.java

+35-54
Original file line numberDiff line numberDiff line change
@@ -16,76 +16,57 @@
1616
* @see SortAlgorithm
1717
*/
1818
class CombSort implements SortAlgorithm {
19+
private static final double SHRINK_FACTOR = 1.3;
1920

20-
// To find gap between elements
21-
private int nextGap(int gap) {
22-
// Shrink gap by Shrink factor
23-
gap = (gap * 10) / 13;
21+
/**
22+
* Method to find the next gap
23+
*
24+
* @param gap the current gap
25+
* @return the next gap value
26+
*/
27+
private int getNextGap(int gap) {
28+
gap = (int) (gap / SHRINK_FACTOR);
2429
return Math.max(gap, 1);
2530
}
2631

2732
/**
28-
* Function to sort arr[] using Comb
33+
* Method to sort the array using CombSort
2934
*
30-
* @param arr - an array should be sorted
31-
* @return sorted array
35+
* @param arr the array to be sorted
36+
* @param <T> the type of elements in the array
37+
* @return the sorted array
3238
*/
3339
@Override
3440
public <T extends Comparable<T>> T[] sort(T[] arr) {
35-
int size = arr.length;
36-
37-
// initialize gap
38-
int gap = size;
39-
40-
// Initialize swapped as true to make sure that loop runs
41+
int gap = arr.length;
4142
boolean swapped = true;
4243

43-
// Keep running while gap is more than 1 and last iteration caused a swap
4444
while (gap != 1 || swapped) {
45-
// Find next gap
46-
gap = nextGap(gap);
47-
48-
// Initialize swapped as false so that we can check if swap happened or not
49-
swapped = false;
50-
51-
// Compare all elements with current gap
52-
for (int i = 0; i < size - gap; i++) {
53-
if (SortUtils.less(arr[i + gap], arr[i])) {
54-
// Swap arr[i] and arr[i+gap]
55-
SortUtils.swap(arr, i, i + gap);
56-
swapped = true;
57-
}
58-
}
45+
gap = getNextGap(gap);
46+
swapped = performSwaps(arr, gap);
5947
}
48+
6049
return arr;
6150
}
6251

63-
// Driver method
64-
public static void main(String[] args) {
65-
CombSort ob = new CombSort();
66-
Integer[] arr = {
67-
8,
68-
4,
69-
1,
70-
56,
71-
3,
72-
-44,
73-
-1,
74-
0,
75-
36,
76-
34,
77-
8,
78-
12,
79-
-66,
80-
-78,
81-
23,
82-
-6,
83-
28,
84-
0,
85-
};
86-
ob.sort(arr);
52+
/**
53+
* Method to perform the swapping of elements in the array based on the current gap
54+
*
55+
* @param arr the array to be sorted
56+
* @param gap the current gap
57+
* @param <T> the type of elements in the array
58+
* @return true if a swap occurred, false otherwise
59+
*/
60+
private <T extends Comparable<T>> boolean performSwaps(final T[] arr, final int gap) {
61+
boolean swapped = false;
62+
63+
for (int i = 0; i < arr.length - gap; i++) {
64+
if (SortUtils.less(arr[i + gap], arr[i])) {
65+
SortUtils.swap(arr, i, i + gap);
66+
swapped = true;
67+
}
68+
}
8769

88-
System.out.println("sorted array");
89-
SortUtils.print(arr);
70+
return swapped;
9071
}
9172
}
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,8 @@
11
package com.thealgorithms.sorts;
22

3-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4-
5-
import org.junit.jupiter.api.Test;
6-
7-
/**
8-
* @author Tabbygray (https://github.com/Tabbygray)
9-
* @see CombSort
10-
*/
11-
12-
public class CombSortTest {
13-
14-
private CombSort combSort = new CombSort();
15-
16-
@Test
17-
public void combSortEmptyArray() {
18-
Integer[] inputArray = {};
19-
Integer[] outputArray = combSort.sort(inputArray);
20-
Integer[] expectedOutput = {};
21-
assertArrayEquals(outputArray, expectedOutput);
22-
}
23-
24-
@Test
25-
public void combSortSingleStringElement() {
26-
String[] inputArray = {"Test"};
27-
String[] outputArray = combSort.sort(inputArray);
28-
String[] expectedArray = {"Test"};
29-
assertArrayEquals(outputArray, expectedArray);
30-
}
31-
32-
@Test
33-
public void combSortStringArray() {
34-
String[] inputArray = {"4gp8", "aBJ2", "85cW", "Pmk9", "ewZO", "meuU", "RhNd", "5TKB", "eDd5", "zzyo"};
35-
String[] outputArray = combSort.sort(inputArray);
36-
String[] expectedArray = {"4gp8", "5TKB", "85cW", "Pmk9", "RhNd", "aBJ2", "eDd5", "ewZO", "meuU", "zzyo"};
37-
assertArrayEquals(outputArray, expectedArray);
38-
}
39-
40-
@Test
41-
public void combSortIntegerArray() {
42-
Integer[] inputArray = {36, 98, -51, -23, 66, -58, 31, 25, -30, 40};
43-
Integer[] outputArray = combSort.sort(inputArray);
44-
Integer[] expectedArray = {-58, -51, -30, -23, 25, 31, 36, 40, 66, 98};
45-
assertArrayEquals(outputArray, expectedArray);
46-
}
47-
48-
@Test
49-
public void combSortDoubleArray() {
50-
Double[] inputArray = {0.8335545399, 0.9346214114, 0.3096396752, 0.6433840668, 0.3973191975, 0.6118850724, 0.0553975453, 0.1961108601, 0.6172800885, 0.1065247772};
51-
Double[] outputArray = combSort.sort(inputArray);
52-
Double[] expectedArray = {
53-
0.0553975453,
54-
0.1065247772,
55-
0.1961108601,
56-
0.3096396752,
57-
0.3973191975,
58-
0.6118850724,
59-
0.6172800885,
60-
0.6433840668,
61-
0.8335545399,
62-
0.9346214114,
63-
};
64-
assertArrayEquals(outputArray, expectedArray);
3+
public class CombSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new CombSort();
657
}
668
}

0 commit comments

Comments
 (0)