forked from hansiming/JavaProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellSort.java
More file actions
50 lines (34 loc) · 761 Bytes
/
ShellSort.java
File metadata and controls
50 lines (34 loc) · 761 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
package com.csdhsm.sort;
/**
* @Title: ShellSort.java
* @Package: com.csdhsm.sort
* @Description 希尔排序
* @author Han
* @date 2016-4-3 下午5:04:01
* @version V1.0
*/
public class ShellSort {
public void sort(int[] arr,int len){
/**
* 步长默认为长度/2
*/
int gap = len/2;
/**
* 当步长gap为1时,结束排序
*/
while(gap >= 1){
// 把距离为 gap 的元素编为一个组,扫描所有组
for(int i = gap;i < len;i ++){
int temp = arr[i];
int j = 0;
// 对距离为 gap 的元素组进行排序
for(j = i-gap;j >= 0 && temp < arr[j];j = j-gap){
arr[i] = arr[j];
}
arr[j + gap] = temp;
}
//减小增量
gap = gap / 2;
}
}
}