forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
38 lines (33 loc) · 1.02 KB
/
ShellSort.java
File metadata and controls
38 lines (33 loc) · 1.02 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
package sort;
public class ShellSort {
/**
* 对数组分组并对每个组做直接插入排序, 完成后缩小组的数量, 重复插入排序, 直到缩小到一个组 第一次分组数: section = n/2 == 0 ? n/2 : n/2+1, 分组规则:
* 每隔n/2挑一个数, 即[x]和[x+n/2]为一组
*/
public void sort(int numbers[]) {
int section = numbers.length / 2;
int j;
int temp;
while (section >= 1) {
for (int i = section; i < numbers.length; i++) {
temp = numbers[i];
j = i - section;
while (j >= 0 && numbers[j] > temp) {
numbers[j + section] = numbers[j];
j = j - section;
}
numbers[j + section] = temp;
}
section /= 2;
}
System.out.print("排序后: ");
for (int x = 0; x < numbers.length; x++) {
System.out.print(numbers[x] + " ");
}
}
public static void main(String[] args) {
int[] numbers = new int[] { 4, 3, 6, 2, 7, 1, 5, 8 };
ShellSort ins = new ShellSort();
ins.sort(numbers);
}
}