Skip to content

Commit 5113101

Browse files
authored
refactor: cleanup ShellSort (#5302)
1 parent ebed8b3 commit 5113101

File tree

2 files changed

+50
-85
lines changed

2 files changed

+50
-85
lines changed

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

+46-23
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,59 @@ public class ShellSort implements SortAlgorithm {
1111
*/
1212
@Override
1313
public <T extends Comparable<T>> T[] sort(T[] array) {
14-
int length = array.length;
15-
int gap = 1;
16-
17-
/* Calculate gap for optimization purpose */
18-
while (gap < length / 3) {
19-
gap = 3 * gap + 1;
14+
if (array.length == 0) {
15+
return array;
2016
}
2117

22-
for (; gap > 0; gap /= 3) {
23-
for (int i = gap; i < length; i++) {
24-
int j;
25-
T temp = array[i];
26-
for (j = i; j >= gap && SortUtils.less(temp, array[j - gap]); j -= gap) {
27-
array[j] = array[j - gap];
28-
}
29-
array[j] = temp;
30-
}
18+
int gap = calculateInitialGap(array.length);
19+
20+
while (gap > 0) {
21+
performGapInsertionSort(array, gap);
22+
gap = calculateNextGap(gap);
3123
}
24+
3225
return array;
3326
}
3427

35-
/* Driver Code */
36-
public static void main(String[] args) {
37-
Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12};
28+
/**
29+
* Calculates the initial gap value using the Knuth sequence.
30+
*
31+
* @param length the length of the array.
32+
* @return the initial gap value.
33+
*/
34+
private int calculateInitialGap(final int length) {
35+
int gap = 1;
36+
while (gap < length / 3) {
37+
gap = 3 * gap + 1;
38+
}
39+
return gap;
40+
}
41+
42+
/**
43+
* Calculates the next gap value.
44+
*
45+
* @param currentGap the current gap value.
46+
* @return the next gap value.
47+
*/
48+
private int calculateNextGap(final int currentGap) {
49+
return currentGap / 3;
50+
}
3851

39-
ShellSort sort = new ShellSort();
40-
sort.sort(toSort);
41-
for (int i = 0; i < toSort.length - 1; ++i) {
42-
assert toSort[i] <= toSort[i + 1];
52+
/**
53+
* Performs an insertion sort for the specified gap value.
54+
*
55+
* @param array the array to be sorted.
56+
* @param gap the current gap value.
57+
* @param <T> the type of elements in the array.
58+
*/
59+
private <T extends Comparable<T>> void performGapInsertionSort(final T[] array, final int gap) {
60+
for (int i = gap; i < array.length; i++) {
61+
T temp = array[i];
62+
int j;
63+
for (j = i; j >= gap && SortUtils.less(temp, array[j - gap]); j -= gap) {
64+
array[j] = array[j - gap];
65+
}
66+
array[j] = temp;
4367
}
44-
SortUtils.print(toSort);
4568
}
4669
}
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-
public class ShellSortTest {
8-
9-
private ShellSort shellSort = new ShellSort();
10-
11-
@Test
12-
public void shellSortEmptyArray() {
13-
Integer[] inputArray = {};
14-
Integer[] outputArray = shellSort.sort(inputArray);
15-
Integer[] expectedOutput = {};
16-
assertArrayEquals(outputArray, expectedOutput);
17-
}
18-
19-
@Test
20-
public void shellSortSingleIntegerArray() {
21-
Integer[] inputArray = {4};
22-
Integer[] outputArray = shellSort.sort(inputArray);
23-
Integer[] expectedOutput = {4};
24-
assertArrayEquals(outputArray, expectedOutput);
25-
}
26-
27-
@Test
28-
public void shellSortSingleStringArray() {
29-
String[] inputArray = {"s"};
30-
String[] outputArray = shellSort.sort(inputArray);
31-
String[] expectedOutput = {"s"};
32-
assertArrayEquals(outputArray, expectedOutput);
33-
}
34-
35-
@Test
36-
public void shellSortNonDuplicateIntegerArray() {
37-
Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36};
38-
Integer[] outputArray = shellSort.sort(inputArray);
39-
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99};
40-
assertArrayEquals(outputArray, expectedOutput);
41-
}
42-
43-
@Test
44-
public void shellSortDuplicateIntegerArray() {
45-
Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23};
46-
Integer[] outputArray = shellSort.sort(inputArray);
47-
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27};
48-
assertArrayEquals(outputArray, expectedOutput);
49-
}
50-
51-
@Test
52-
public void shellSortNonDuplicateStringArray() {
53-
String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"};
54-
String[] outputArray = shellSort.sort(inputArray);
55-
String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"};
56-
assertArrayEquals(outputArray, expectedOutput);
57-
}
58-
59-
@Test
60-
public void shellSortDuplicateStringArray() {
61-
String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"};
62-
String[] outputArray = shellSort.sort(inputArray);
63-
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
64-
assertArrayEquals(outputArray, expectedOutput);
3+
public class ShellSortTest extends SortingAlgorithmTest {
4+
@Override
5+
SortAlgorithm getSortAlgorithm() {
6+
return new ShellSort();
657
}
668
}

0 commit comments

Comments
 (0)