forked from qwhai/algorithms-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
47 lines (41 loc) · 1.08 KB
/
ShellSort.java
File metadata and controls
47 lines (41 loc) · 1.08 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
package shell;
import interf.SortImpl;
/**
* Shell 排序
*/
public class ShellSort extends SortImpl {
@Override
public void sort(int[] arr) {
int step = arr.length;
do {
step = step / 3 + 1;
insert(arr, step);
} while (step > 1);
}
/**
* 内部参数释义
* i 分组内索引
* j 插入排序中的分界线索引
* k 插入排序分界线索引之前的索引
*
* @param arr
* 待排序数组
* @param step
* 分组步长
*/
private void insert(int[] arr, int step) {
for (int i = 0; i < step; i++) {
for (int j = i + step; j < arr.length; j += step) {
int x = arr[j], k;
for (k = j - step; k >= 0; k -= step) {
if (x < arr[k]) {
arr[k + step] = arr[k]; // or coding like this -> swap(arr, k, k + step);
} else {
break;
}
}
arr[k + step] = x;
}
}
}
}