Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Issue #554 Implemented ShellSort #555

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
*/
package com.williamfiset.algorithms.sorting;

import java.util.*;

public class Heapsort implements InplaceSort {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public class MergeSort implements InplaceSort {
@Override
public void sort(int[] values) {
int[] sortedValues = MergeSort.mergesort(values);
for (int i = 0; i < values.length; i++) {
values[i] = sortedValues[i];
}
System.arraycopy(sortedValues, 0, values, 0, values.length);
}

public static int[] mergesort(int[] ar) {
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/williamfiset/algorithms/sorting/ShellSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Shell sort implementation
*
* <p>Run with:
*
* <p>$ ./gradlew run -Palgorithm=sorting.ShellSort
*
* @author Tan Karageldi, [email protected]
*/
package com.williamfiset.algorithms.sorting;

public class ShellSort implements InplaceSort{

@Override
public void sort(int[] values) {
ShellSort.shellSort(values);
}

// The idea of shell sort is sorting the elements that are far away from each other
// defined by the interval (inter) and progressively reducing the gap between elements,
// to do an insertion sort when the gap is 1.

public static void shellSort(int[] array){
int n = array.length;
for(int inter = n/2 ; inter > 0 ; inter /= 2){
for(int i = inter; i< n; i+= 1){
int temp = array[i];
int j;
for(j = i; j >= inter && array[j - inter] > temp; j -= inter){
array[j] = array[j - inter];
}
array[j] = temp;
}
}

}

public static void main(String[] args) {
InplaceSort sorter = new ShellSort();
int[] array = {10, 4, 6, 8, -13, 2, 3};
sorter.sort(array);
// Prints:
// [-13, 2, 3, 4, 6, 8, 10]
System.out.println(java.util.Arrays.toString(array));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.williamfiset.algorithms.sorting;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;

public class ShellSortTest {

@Test
public void testPositiveNumbers() {
int[] input = {5, 2, 9, 1, 5, 6};
int[] expected = {1, 2, 5, 5, 6, 9};
ShellSort.shellSort(input);
assertArrayEquals(expected, input);
}

@Test
public void testNegativeNumbers() {
int[] input = {-3, -1, -4, -2, -5};
int[] expected = {-5, -4, -3, -2, -1};
ShellSort.shellSort(input);
assertArrayEquals(expected, input);
}

@Test
public void testMixedNumbers() {
int[] input = {10, -3, 0, 2, -15, 7};
int[] expected = {-15, -3, 0, 2, 7, 10};
ShellSort.shellSort(input);
assertArrayEquals(expected, input);
}
@Test
public void testSingleElementArray() {
int[] input = {42};
int[] expected = {42};
ShellSort.shellSort(input);
assertArrayEquals(expected, input);
}
@Test
public void testEmptyArray() {
int[] input = {};
int[] expected = {};
ShellSort.shellSort(input);
assertArrayEquals(expected, input);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.williamfiset.algorithms.sorting;

import static com.google.common.truth.Truth.assertThat;

import com.williamfiset.algorithms.utils.TestUtils;
import java.util.Arrays;
import java.util.EnumSet;

import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;
import com.williamfiset.algorithms.utils.TestUtils;

// Test all sorting algorithms under various constraints.
//
// Not all sorting algorithms are suitable for every type for test. For instance,
Expand All @@ -25,6 +26,7 @@ enum SortingAlgorithm {
QUICK_SORT(new QuickSort()),
QUICK_SORT3(new QuickSort3()),
RADIX_SORT(new RadixSort()),
SHELL_SORT(new ShellSort()),
SELECTION_SORT(new SelectionSort());

private InplaceSort algorithm;
Expand All @@ -49,6 +51,7 @@ public InplaceSort getSortingAlgorithm() {
SortingAlgorithm.QUICK_SORT,
SortingAlgorithm.QUICK_SORT3,
SortingAlgorithm.RADIX_SORT,
SortingAlgorithm.SHELL_SORT,
SortingAlgorithm.SELECTION_SORT);

@Test
Expand Down