-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
43 lines (40 loc) · 956 Bytes
/
ShellSort.java
File metadata and controls
43 lines (40 loc) · 956 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
package sort;
/**
* @author ahscuml
* @date 2018/7/12
* @time 10:01
*/
public class ShellSort {
private ShellSort() {
}
/**
* 希尔排序算法
*/
public static void sort(Comparable[] arr) {
int n = arr.length;
int h = 1;
while (h < n / 3) {
h = 3 * h + 1;
}
while (h >= 1) {
// 排序是从h到n
for (int i = h; i < n; i++) {
Comparable e = arr[i];
int j = i;
for (; j >= h && e.compareTo(arr[j - h]) < 0; j -= h) {
arr[j] = arr[j - h];
}
arr[j] = e;
}
h /= 3;
}
}
/**
* 测试用例
*/
public static void main(String[] args) {
int N = 1000000;
Integer[] arr = SortTestHelper.generateRandomArray(N, 0, 100000);
SortTestHelper.testSort("sort.ShellSort", arr);
}
}