-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
51 lines (46 loc) · 917 Bytes
/
Copy pathShellSort.java
File metadata and controls
51 lines (46 loc) · 917 Bytes
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
/**
*
* @author KAKANAKOU MIGUEL
*
*/
public final class ShellSort {
/**
* Use the shell sort method to sort the given array A
*
* @param A Array of Comparable that contains the object to sort
*/
public static void sort(Comparable[] A) {
if (A == null)
return;
int h = computeMaxH(A.length);
while (h >= 1) {
hInsertionSort(A, h);
h = h / 3;
}
}
private static void hInsertionSort(Comparable[] A, int h) {
Comparable curVal;
int j;
for (int i = h; i < A.length; i = i + h) {
curVal = A[i];
j = i - h;
while ((j >= 0) && less(curVal, A[j])) {
A[j + h] = A[j];
j = j - h;
}
A[j + h] = curVal;
}
}
private static int computeMaxH(int len) {
int h = 1;
while (h < len / 3)
h = 3 * h + 1;
return h;
}
private static boolean less(Comparable a, Comparable b) {
int cmp = a.compareTo(b);
if (cmp < 0)
return true;
return false;
}
}