forked from joyang1/JavaInterview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
35 lines (29 loc) · 899 Bytes
/
Copy pathShellSort.java
File metadata and controls
35 lines (29 loc) · 899 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
package sort;
import java.util.Arrays;
/**
* Created by TommyYang on 2018/3/12.
*/
public class ShellSort implements IArraySort {
@Override
public int[] sort(int[] sourArr) throws Exception {
// 对 sourceArr 进行拷贝,不改变参数内容
int[] desArr = Arrays.copyOf(sourArr, sourArr.length);
int gap = 1;
while (gap < desArr.length / 4){ //动态定义间隔序列
gap = gap * 4 + 1;
}
while (gap > 0){
for (int i = gap; i < desArr.length; i++){
int tmp = desArr[i];
int j = i - gap;
while (j >= 0 && desArr[j] > tmp){
desArr[j + gap] = desArr[j];
j -= gap;
}
desArr[j + gap] = tmp;
}
gap = (int)Math.floor(gap / 3);
}
return desArr;
}
}