-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
44 lines (41 loc) · 1.1 KB
/
ShellSort.java
File metadata and controls
44 lines (41 loc) · 1.1 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
package sort;
import java.util.Arrays;
/**
* @ProjectName: leetcode
* @Package: sort
* @ClassName: ShellSort
* @Author: markey
* @Description:
* @Date: 2020/3/18 23:39
* @Version: 1.0
*/
public class ShellSort implements Sort {
@Override
public int[] sort(int[] array) {
System.out.println(Arrays.toString(array));
int gap = array.length / 2;
while (gap > 0) {
System.out.println("gap:" + gap);
for (int i = 0; i < gap; i++) {
sort(array, i, gap);
}
System.out.println(Arrays.toString(array));
gap /= 2;
}
return array;
}
// 插入排序
private void sort(int[] array, int start, int gap) {
for (int i = start; i < array.length; i += gap) {
int temp = array[i];
for (int j = i; j >= 0; j-= gap) {
if (j - gap > 0 && array[j - gap] > temp) {
array[j] = array[j - gap];
} else {
array[j] = temp;
break;
}
}
}
}
}