Skip to content

Commit 64c6917

Browse files
committed
[A] 添加希尔排序md
1 parent 1b2244b commit 64c6917

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
## 前言
2+
3+
希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。
4+
5+
希尔排序是基于插入排序的以下两点性质而提出改进方法的:
6+
7+
- 插入排序在对几乎已经排好序的数据操作时,效率高,即可以达到线性排序的效率;
8+
- 但插入排序一般来说是低效的,因为插入排序每次只能将数据移动一位;
9+
10+
希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序,待整个序列中的记录"基本有序"时,再对全体记录进行依次直接插入排序。
11+
12+
动态图:
13+
14+
![希尔排序动态图](https://github.com/coderbruis/AlgorithmsInJava/blob/master/notes/pictures/shellSort.gif)
15+
16+
17+
## 正文
18+
19+
### 代码实现
20+
21+
```
22+
public class ShellSort {
23+
public static void sort(int[] arr) {
24+
int n = arr.length;
25+
26+
int h = 1;
27+
28+
while (h < n / 3) {
29+
h = 3 * h + 1;
30+
}
31+
32+
while (h >= 1) {
33+
for (int i = h; i < n; i++) {
34+
int e = arr[i];
35+
int j = i;
36+
for (; j >= h && e < arr[j-h]; j -= h) {
37+
arr[j] = arr[j-h];
38+
}
39+
arr[j] = e;
40+
}
41+
42+
h /= 3;
43+
}
44+
}
45+
}
46+
```

notes/pictures/shellSort.gif

271 KB
Loading

0 commit comments

Comments
 (0)