refactor: cleanup ShellSort (#5302)

This commit is contained in:
Alex Klymenko
2024-07-26 09:55:11 +03:00
committed by GitHub
parent ebed8b38b8
commit 5113101e5d
2 changed files with 51 additions and 86 deletions

View File

@ -11,36 +11,59 @@ public class ShellSort implements SortAlgorithm {
*/ */
@Override @Override
public <T extends Comparable<T>> T[] sort(T[] array) { public <T extends Comparable<T>> T[] sort(T[] array) {
int length = array.length; if (array.length == 0) {
int gap = 1; return array;
/* Calculate gap for optimization purpose */
while (gap < length / 3) {
gap = 3 * gap + 1;
} }
for (; gap > 0; gap /= 3) { int gap = calculateInitialGap(array.length);
for (int i = gap; i < length; i++) {
int j; while (gap > 0) {
T temp = array[i]; performGapInsertionSort(array, gap);
for (j = i; j >= gap && SortUtils.less(temp, array[j - gap]); j -= gap) { gap = calculateNextGap(gap);
array[j] = array[j - gap];
}
array[j] = temp;
}
} }
return array; return array;
} }
/* Driver Code */ /**
public static void main(String[] args) { * Calculates the initial gap value using the Knuth sequence.
Integer[] toSort = {4, 23, 6, 78, 1, 54, 231, 9, 12}; *
* @param length the length of the array.
ShellSort sort = new ShellSort(); * @return the initial gap value.
sort.sort(toSort); */
for (int i = 0; i < toSort.length - 1; ++i) { private int calculateInitialGap(final int length) {
assert toSort[i] <= toSort[i + 1]; int gap = 1;
while (gap < length / 3) {
gap = 3 * gap + 1;
}
return gap;
}
/**
* Calculates the next gap value.
*
* @param currentGap the current gap value.
* @return the next gap value.
*/
private int calculateNextGap(final int currentGap) {
return currentGap / 3;
}
/**
* Performs an insertion sort for the specified gap value.
*
* @param array the array to be sorted.
* @param gap the current gap value.
* @param <T> the type of elements in the array.
*/
private <T extends Comparable<T>> void performGapInsertionSort(final T[] array, final int gap) {
for (int i = gap; i < array.length; i++) {
T temp = array[i];
int j;
for (j = i; j >= gap && SortUtils.less(temp, array[j - gap]); j -= gap) {
array[j] = array[j - gap];
}
array[j] = temp;
} }
SortUtils.print(toSort);
} }
} }

View File

@ -1,66 +1,8 @@
package com.thealgorithms.sorts; package com.thealgorithms.sorts;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class ShellSortTest extends SortingAlgorithmTest {
@Override
import org.junit.jupiter.api.Test; SortAlgorithm getSortAlgorithm() {
return new ShellSort();
public class ShellSortTest {
private ShellSort shellSort = new ShellSort();
@Test
public void shellSortEmptyArray() {
Integer[] inputArray = {};
Integer[] outputArray = shellSort.sort(inputArray);
Integer[] expectedOutput = {};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortSingleIntegerArray() {
Integer[] inputArray = {4};
Integer[] outputArray = shellSort.sort(inputArray);
Integer[] expectedOutput = {4};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortSingleStringArray() {
String[] inputArray = {"s"};
String[] outputArray = shellSort.sort(inputArray);
String[] expectedOutput = {"s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortNonDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 99, 27, -15, 23, -36};
Integer[] outputArray = shellSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 27, 99};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortDuplicateIntegerArray() {
Integer[] inputArray = {6, -1, 27, -15, 23, 27, -36, 23};
Integer[] outputArray = shellSort.sort(inputArray);
Integer[] expectedOutput = {-36, -15, -1, 6, 23, 23, 27, 27};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortNonDuplicateStringArray() {
String[] inputArray = {"s", "b", "k", "a", "d", "c", "h"};
String[] outputArray = shellSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "c", "d", "h", "k", "s"};
assertArrayEquals(outputArray, expectedOutput);
}
@Test
public void shellSortDuplicateStringArray() {
String[] inputArray = {"s", "b", "d", "a", "d", "c", "h", "b"};
String[] outputArray = shellSort.sort(inputArray);
String[] expectedOutput = {"a", "b", "b", "c", "d", "d", "h", "s"};
assertArrayEquals(outputArray, expectedOutput);
} }
} }