-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathShellSort.java
More file actions
51 lines (35 loc) · 1017 Bytes
/
ShellSort.java
File metadata and controls
51 lines (35 loc) · 1017 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
package sort;
/**
* Created by ozc on 2018/3/26.
*
* @author ozc
* @version 1.0
*/
public class ShellSort {
public static void main(String[] args) {
int[] arrays = {6, 4321, 432, 344, 55 };
shellSort(arrays);
System.out.println("公众号:Java3y" + arrays);
}
/**
* 希尔排序
*
* @param arrays
*/
public static void shellSort(int[] arrays) {
//增量每次都/2
for (int step = arrays.length / 2; step > 0; step /= 2) {
//从增量那组开始进行插入排序,直至完毕
for (int i = step; i < arrays.length; i++) {
int j = i;
int temp = arrays[j];
// j - step 就是代表与它同组隔壁的元素
while (j - step >= 0 && arrays[j - step] > temp) {
arrays[j] = arrays[j - step];
j = j - step;
}
arrays[j] = temp;
}
}
}
}