Skip to content

Commit 3e3d1e4

Browse files
added shellsort algorithm
1 parent f61f449 commit 3e3d1e4

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ gradle clean assemble
3434
## Available algorithms
3535
- [Insertion Sort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/InsertionSort.java)
3636
- [Selection Sort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/SelectionSort.java)
37+
- [Shellsort](https://github.com/marioluan/java-sorting-algorithms/blob/master/src/main/java/io/github/marioluan/algorithms/sorting/ShellSort.java)
3738

3839
## References
3940
- [https://www.coursera.org/learn/algorithms-part1](https://www.coursera.org/learn/algorithms-part1)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.github.marioluan.algorithms.sorting;
2+
3+
/**
4+
* In-place, non-stable implementation of shellsort algorithm. <br>
5+
* <strong>Time complexity:</strong> O(N^1.5) <br>
6+
* <strong>Space complexity:</strong> O(1) <br>
7+
* References:
8+
* <ul>
9+
* <li><a>https://www.coursera.org/learn/algorithms-part1</a>.</li>
10+
* <li><a>https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif</a></li>
11+
* </ul>
12+
*
13+
* @author marioluan
14+
*/
15+
public class ShellSort extends Sortable {
16+
17+
private static final int C = 3;
18+
19+
/**
20+
* Sort the items from array a.
21+
*
22+
* @param a
23+
*/
24+
@SuppressWarnings("rawtypes")
25+
public static void sort(Comparable[] a) {
26+
int n = a.length;
27+
int h = 1;
28+
29+
// 3x+1 increment sequence
30+
while (h < n / C)
31+
h = (C * h) + 1; // 1, 4, 13, 40, 121, 363, ...
32+
33+
while (h >= 1) {
34+
// h-sort the array
35+
// insertion sort
36+
for (int i = h; i < n; i++) {
37+
for (int j = i; j >= h && less(a[j], a[j - h]); j = j - h)
38+
swap(a, j, j - h);
39+
40+
}
41+
42+
h = h / C;
43+
}
44+
45+
}
46+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package io.github.marioluan.algorithms.sorting;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static com.greghaskins.spectrum.Spectrum.describe;
5+
import static com.greghaskins.spectrum.Spectrum.beforeEach;
6+
import static com.greghaskins.spectrum.Spectrum.it;
7+
8+
import java.util.Arrays;
9+
import java.util.Random;
10+
11+
import org.junit.runner.RunWith;
12+
13+
import com.greghaskins.spectrum.Spectrum;
14+
15+
import io.github.marioluan.algorithms.test.support.SupportHelper;
16+
17+
@SuppressWarnings("rawtypes")
18+
@RunWith(Spectrum.class)
19+
public class ShellSortTest {
20+
21+
private static final Random RANDOM = new Random();
22+
private Comparable[] array = null;
23+
private Comparable[] clone = null;
24+
private int n = 0;
25+
26+
{
27+
describe("ShellSort", () -> {
28+
describe(".sort", () -> {
29+
30+
describe("when array is small", () -> {
31+
beforeEach(() -> {
32+
n = RANDOM.nextInt(7) + 1;
33+
array = SupportHelper.buildRandomArray(n);
34+
clone = array.clone();
35+
});
36+
37+
it("sorts it", () -> {
38+
Arrays.sort(clone);
39+
ShellSort.sort(array);
40+
41+
assertArrayEquals(array, clone);
42+
});
43+
});
44+
45+
describe("when array is huge", () -> {
46+
beforeEach(() -> {
47+
n = RANDOM.nextInt((int) Math.pow(10, 5)) + 1;
48+
array = SupportHelper.buildRandomArray(n);
49+
clone = array.clone();
50+
});
51+
52+
it("sorts it", () -> {
53+
Arrays.sort(clone);
54+
ShellSort.sort(array);
55+
56+
assertArrayEquals(array, clone);
57+
});
58+
});
59+
60+
describe("when array is partially sorted", () -> {
61+
beforeEach(() -> {
62+
array = new Comparable[] { 1, 2, 3, 6, 9, 5, 10, 4, 11,
63+
13, 14, 15, 16, 17, 18, 19, 20 };
64+
clone = array.clone();
65+
});
66+
67+
it("sorts it", () -> {
68+
Arrays.sort(clone);
69+
ShellSort.sort(array);
70+
71+
assertArrayEquals(array, clone);
72+
});
73+
});
74+
75+
});
76+
});
77+
}
78+
}

0 commit comments

Comments
 (0)