forked from OneCodeMonkey/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
71 lines (62 loc) · 1.5 KB
/
ShellSort.java
File metadata and controls
71 lines (62 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Shell Sort
*
* The `ShellSort` class provides static method for sorting an array using Shellsort
* with Knuth's increment sequence(1, 4, 13, 40, ...)
*/
public class Shell {
private Shell() {}
// Rearranges the array in ascending order, using natural order.
public static void sort(Comparable[] a) {
int n = a.length;
// 3x + 1 Knuth's increment sequence: 1, 4, 13, 40, 121, 364,, 1093, 3280,...
int h = 1;
while(h < n / 3)
h = 3 * h + 1;
while(h >= 1) {
// h-sort the array
for(int i = h; i < n; i++) {
for(int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
exchange(a, j, j - h);
}
}
assert isHsorted(a, h);
h /= 3;
}
assert isSorted(a);
}
// helper functions
// is v < w?
private static boolean less(Comparable v, Comparable w) {
return v.compareTo(w) < 0;
}
private static void exchange(Object[] a, int i, int j) {
Object swap = a[i];
a[i] = a[j];
a[j] = swap;
}
// check if the array is sorted
private static function isSorted(Comparable[] a) {
for(int i = 1; i < a.length; i++)
if(less(a[i], a[i - 1]))
return false;
return true;
}
private static boolean isHsorted(Comparable[] a, int h) {
for(int i = h; i < a.length; i++)
if(less(a[i], a[i - h]))
return false;
return true;
}
// print
private static void show(Comparable[] a) {
for(int i = 0; i < a.length; i++)
StdOut.println(a[i]);
}
// test
public static void main(String[] args) {
String[] a = StdIn.readAllStrings();
Shell.sort(a);
show(a);
}
}