# 希尔排序

**希尔排序**（Shellsort）是一种原地比较排序算法。它改进了[插入排序](https://www.edgepedia.org/zh/insertion-sort)：先对彼此相隔固定距离（即间隔）的元素对进行排序，然后以逐步缩小的间隔重复这一过程，直到最后一趟间隔为 1 的排序完成整个排序。唐纳德·L·谢尔（Donald L. Shell）于1959年7月在《ACM通讯》（Communications of the ACM）上发表了该算法，它也被称为递减增量排序。<sup>[1](https://opendsa.cs.vt.edu/ODSA/Books/jmu/cs240-cd3ab25b-1e57-426f-812a-5a34fb2dcf99/fall-2018/section_2/html/Shellsort.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由唐纳德·L·谢尔发表于《ACM通讯》2(7):30–32，1959年7月；首个递减增量排序<sup>[2](https://xlinux.nist.gov/dads/HTML/shellsort.html)</sup> |
| 机制 | 每一趟对数组的 h 个等间隔子序列（相隔 h 个位置的元素）进行插入排序；最后一趟 h = 1 是普通插入排序<sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup> |
| 性质 | 原地（额外内存 Θ(1)）且不稳定<sup>[4](https://www.eecs.umich.edu/courses/eecs380/ALG/niemann/s_shl.htm)</sup> |
| 已知最佳最坏情况 | 采用普拉特（Pratt）的全由 2<sup>p</sup>·3<sup>q</sup> 形式数构成的序列，为 Θ(N log² N)，代价是 Θ(log² N) 趟和很大的常数因子<sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup> |
| 实际最坏情况 | 采用塞奇威克（Sedgewick）1986年的序列 1, 8, 23, 77, 281, ...，为 O(N^(4/3))。<sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup><sup> • </sup><sup>[20](https://collaborate.princeton.edu/en/publications/a-new-upper-bound-for-shellsort/)</sup> |
| 一般下界 | 最坏情况 Ω(N log² N / (log log N)²)，因此希尔排序相对于比较排序的 Θ(N log N) 最优值而言是次优的<sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup> |
| 平均情况（实测） | 在韦斯（Weiss）1991年的研究中，克努特（Knuth）的增量序列为 Θ(N<sup>5/4</sup>)，一个塞奇威克序列为 Θ(N<sup>7/6</sup>)<sup>[6](https://academic.oup.com/comjnl/article-abstract/34/1/88/428141)</sup> |

## 算法如何工作

输入接近有序时，插入排序很快，但一般情况下很慢，因为一个远离其最终位置的元素一次只能移动一个位置。希尔排序利用第一个性质来解决第二个问题：它对由相隔固定间隔 h 的元素组成的子序列多次运行插入排序，每一趟都减小 h，直到 h = 1。<sup>[1](https://opendsa.cs.vt.edu/ODSA/Books/jmu/cs240-cd3ab25b-1e57-426f-812a-5a34fb2dcf99/fall-2018/section_2/html/Shellsort.html)</sup><sup> • </sup><sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup>

间隔为 h 的一趟将数组分成 h 个子序列，S<sub>i</sub> = (s<sub>i</sub>, s<sub>i+h</sub>, s<sub>i+2h</sub>, ...)，并对每个子序列进行插入排序，使数组成为 h 有序。一个重要性质是：对 k 有序数组进行 h 排序后，它仍然 k 有序，因此先前的工作不会被后面的趟破坏。最后一趟 h = 1 是普通插入排序，保证结果完全有序；前面各趟的目的是消除长距离无序，使这一代价高昂的最后一趟几乎没有多少工作要做。<sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup><sup> • </sup><sup>[7](https://www.researchgate.net/publication/366821574_Optimization_Perspectives_on_Shellsort)</sup>

其效率来自能快速移动那些偏离最终位置很远的元素，且只需少量关键字比较，这是普通插入排序做不到的。<sup>[8](https://www.cs.uah.edu/~mdb0013/Sorting/ShellSort.html)</sup> 该算法原地运行，且不稳定，也就是说相等元素可能改变相对次序。<sup>[4](https://www.eecs.umich.edu/courses/eecs380/ALG/niemann/s_shl.htm)</sup>

## 间隔序列问题

运行时间极大地取决于所选的间隔序列，自1959年以来，选择一个好的间隔序列一直是核心设计问题。<sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup><sup> • </sup><sup>[7](https://www.researchgate.net/publication/366821574_Optimization_Perspectives_on_Shellsort)</sup> 谢尔最初提出从 ⌊N/2⌋ 开始，每一趟减半；这种二次幂选择可能导致二次运行时间，因为在最后一趟 1 排序之前，奇数位置的元素从不与偶数位置的元素比较。<sup>[9](https://uisacad5.uis.edu/~broge2/dsa/lecture4/lecture4.html)</sup><sup> • </sup><sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup>

**已知界。** 沃恩·普拉特（Vaughan Pratt）在其1972年斯坦福学位论文《希尔排序与排序网络》（*Shellsort and Sorting Networks*）中证明，所有形如 2<sup>p</sup>·3<sup>q</sup> 的数构成的序列可产生 Θ(N log² N) 的最坏情况时间，这仍是任何增量序列已知的最佳渐近界，并且对于近似整数比几何级数的序列，该界是紧的。<sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup><sup> • </sup><sup>[10](https://books.google.com/books/about/Shellsort_and_Sorting_Networks.html?id=IsA-AAAAIAAJ)</sup> 帕佩尔诺夫–斯塔谢维奇（Papernov–Stasevich）序列 1, 3, 7, 15, 31, ... 给出 O(N<sup>3/2</sup>)，普拉特证明该界对近乎几何的序列是紧的。塞奇威克在1986年用序列 1, 8, 23, 77, 281, ...（形如 4<sup>j+1</sup> + 3·2<sup>j</sup> + 1 的项）打破了 3/2 指数，给出 O(N<sup>4/3</sup>)；因切尔皮（Incerpi）和塞奇威克在1985年证明，对任意 ε > 0，存在一个使用 O(log N) 趟、最坏情况为 O(N<sup>1+ε/√log N</sup>) 的序列，不过这些构造因常数很大而不实用。<sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup><sup> • </sup><sup>[11](https://arxiv.org/abs/2607.08997)</sup>

与此相对，最坏情况的一般下界 Ω(N log² N / (log log N)²) 表明，希尔排序无法达到比较排序的 Θ(N log N) 最优值；姜（Jiang）、李（Li）和维塔尼（Vitányi）证明，采用任意增量序列的 p 趟希尔排序平均需要 Ω(pN<sup>1+1/p</sup>) 次数据移动和比较。<sup>[3](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)</sup><sup> • </sup><sup>[12](https://arxiv.org/abs/cs/9901010)</sup> 韦斯和塞奇威克1988年的下界工作提示，可能没有任何增量序列能使希尔排序达到最优。<sup>[13](https://www.cs.princeton.edu/research/techreps/665)</sup>

**实用序列。** 由于普拉特序列需要太多趟，实际实现使用具有 O(log N) 个增量的近似几何序列。克努特推荐 1, 4, 13, 40, 121, 364, ...（每一项为 3h + 1），它易于计算，经验表现良好；后来的研究发现，对几何序列而言约 11/5 的增长率最佳。<sup>[5](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)</sup><sup> • </sup><sup>[14](https://algs4.cs.princeton.edu/code/javadoc/edu/princeton/cs/algs4/Shell.html)</sup> 德田（Tokuda）1992年的序列使用 9/4 的增长率，马尔钦·丘拉（Marcin Ciura）2001年的论文通过最小化比较次数而非交换次数，用实验方法找到了好的增量，这改变了性能的衡量方式。<sup>[7](https://www.researchgate.net/publication/366821574_Optimization_Perspectives_on_Shellsort)</sup> 斯基恩（Skean）、埃伦堡（Ehrenborg）和亚罗姆奇克（Jaromczyk）2023年的一项研究对序列生成函数进行参数网格搜索，发现有些序列在小数组的比较次数上胜过最先进的间隔序列，在大数组上胜过德田序列的运行时间。<sup>[15](https://arxiv.org/abs/2301.00316)</sup>

## 平均情况行为

平均情况一直是一个长期未解决的问题。克努特早期的实证研究对若干序列给出的运行时间介于 Θ(N<sup>1.25</sup>) 和 Θ(N<sup>1.28</sup>) 之间，韦斯表明这些形式对大规模输入并不准确。他1991年的研究在 250 ≤ N < 100,000 时拟合精度在1%以内，对更大的 N 约为0.1%，发现克努特建议的所有序列均为 Θ(N<sup>5/4</sup>)，一个塞奇威克序列为 Θ(N<sup>7/6</sup>)。这些结果提示，长度为 O(log N)、最坏情况时间为 Θ(N<sup>k</sup>) 的序列，其平均情况时间为 Θ(N<sup>(k+1)/2</sup>)，并且没有任何增量序列能使希尔排序即使在平均情况下也达到 O(N log N)。<sup>[6](https://academic.oup.com/comjnl/article-abstract/34/1/88/428141)</sup>

## 实际性能

对于中等规模和接近有序的文件，希尔排序具有竞争力；其简单、原地运行且不使用递归的特点，使它对嵌入式环境很有吸引力。<sup>[13](https://www.cs.princeton.edu/research/techreps/665)</sup><sup> • </sup><sup>[16](https://ece.uwaterloo.ca/~dwharder/icsrts/Lecture_materials/A_practical_introduction_to_real-time_systems_for_undergraduate_engineering.pdf)</sup> 在 OpenDSA 于 3.4 GHz 英特尔奔腾4（Intel Pentium 4）上的一项基准测试中，基本希尔排序排序一百万个随机整数耗时 554 毫秒，而采用以除以三为基础的增量的版本耗时 530 毫秒；相比之下，优化[快速排序](https://www.edgepedia.org/zh/quicksort)为 162 毫秒，归并排序为 219 毫秒，插入排序为 674,420 毫秒。对于由 10,000 个已排序元素组成的输入，希尔排序耗时 0.44 毫秒，而插入排序为 129.05 毫秒。<sup>[17](https://opendsa-server.cs.vt.edu/ODSA/Books/duke/cs201/spring-2025/MW/html/SortingEmpirical.html)</sup> 一本滑铁卢（Waterloo）实时系统教材称，对于最多约一百万个元素的列表，希尔排序相当快。<sup>[16](https://ece.uwaterloo.ca/~dwharder/icsrts/Lecture_materials/A_practical_introduction_to_real-time_systems_for_undergraduate_engineering.pdf)</sup>

希尔排序也被用作排序网络，一种并行排序结构。赛弗（Cypher）在1993年证明，所有采用单调递减增量的希尔排序网络都需要 Ω(N log² N / log log N) 个比较器，几乎匹配普拉特的 O(N log² N) 上界。<sup>[18](https://epubs.siam.org/doi/10.1137/0222006)</sup>

## 近期进展

下界研究一直在继续，以逼近实用序列。臧正翰（Zhenghan Zang）2026年的一篇预印本引入了一种使用参数化多项式映射的势函数分析，并证明采用德田1992年间隔序列的希尔排序最坏情况时间至少为 Ω(N<sup>1.26</sup>)，并推广到与有理几何级数相差固定距离内的任意严格递减序列。<sup>[11](https://arxiv.org/abs/2607.08997)</sup> 同样在2026年，刘波（Bo Liu）报告了一项对可执行间隔生成器的强化学习搜索，产生了序列 1, 3, 8, 20, 47, 116, 300, 585, 1416, 3303, ...，该序列在 10<sup>7</sup> < N ≤ 10<sup>8</sup> 的25个大型任务上，在七个经典基线中取得了最低的平均操作次数，并具有约 N<sup>1.0243</sup> 的匹配上下指数（忽略多对数因子），这比短稀疏序列的经典 N<sup>4/3</sup> 界低了一步。<sup>[19](https://arxiv.org/abs/2609.29881)</sup>

## References

1. [Shellsort, CS 240 Data Structures and Algorithms, OpenDSA (James Madison University).](https://opendsa.cs.vt.edu/ODSA/Books/jmu/cs240-cd3ab25b-1e57-426f-812a-5a34fb2dcf99/fall-2018/section_2/html/Shellsort.html)
2. [Paul E. Black and Art S. Kagel, "Shell sort", Dictionary of Algorithms and Data Structures, NIST.](https://xlinux.nist.gov/dads/HTML/shellsort.html)
3. [I. Radavičius, M. Baranauskas (2013). An empirical study of the gap sequences for Shell sort. Liet. matem. rink. Proc. LMS Ser. A 54:61–66.](https://www.journals.vu.lt/LMR/en/article/download/14899/13917/23072)
4. [Thomas Niemann, Sorting and Searching Algorithms: Shell Sort, EECS 380, University of Michigan.](https://www.eecs.umich.edu/courses/eecs380/ALG/niemann/s_shl.htm)
5. [Robert Sedgewick (1996). Analysis of Shellsort and Related Algorithms.](https://sedgewick.io/wp-content/themes/sedgewick/papers/1996Shellsort.pdf)
6. [M. A. Weiss (1991). Empirical Study of the Expected Running Time of Shellsort. The Computer Journal 34(1):88–91.](https://academic.oup.com/comjnl/article-abstract/34/1/88/428141)
7. [Oscar Skean, Richard Ehrenborg, Jerzy W. Jaromczyk (2023). Optimization Perspectives on Shellsort. arXiv:2301.00316.](https://www.researchgate.net/publication/366821574_Optimization_Perspectives_on_Shellsort)
8. [Shell Sort, University of Alabama in Huntsville course notes.](https://www.cs.uah.edu/~mdb0013/Sorting/ShellSort.html)
9. [Introduction to Searching and Sorting, University of Illinois Springfield course notes.](https://uisacad5.uis.edu/~broge2/dsa/lecture4/lecture4.html)
10. [Vaughan R. Pratt (1972). Shellsort and Sorting Networks. Stanford University.](https://books.google.com/books/about/Shellsort_and_Sorting_Networks.html?id=IsA-AAAAIAAJ)
11. [Zhenghan Zang (2026). Improved Lower Bounds of the Time Complexity of Shellsort. arXiv:2607.08997.](https://arxiv.org/abs/2607.08997)
12. [Average-Case Complexity of Shellsort. arXiv:cs/9901010.](https://arxiv.org/abs/cs/9901010)
13. [Robert Sedgewick, Mark Allen Weiss (1988). Tight Lower Bounds for Shellsort. Princeton CS TR-137-88.](https://www.cs.princeton.edu/research/techreps/665)
14. [edu.princeton.cs.algs4.Shell Javadoc, Algorithms 4th Edition.](https://algs4.cs.princeton.edu/code/javadoc/edu/princeton/cs/algs4/Shell.html)
15. [Oscar Skean, Richard Ehrenborg, Jerzy W. Jaromczyk (2023). Optimization Perspectives on Shellsort. arXiv:2301.00316.](https://arxiv.org/abs/2301.00316)
16. [D. W. Harder, A Practical Introduction to Real-Time Systems, University of Waterloo, Sorting Algorithms appendix.](https://ece.uwaterloo.ca/~dwharder/icsrts/Lecture_materials/A_practical_introduction_to_real-time_systems_for_undergraduate_engineering.pdf)
17. [An Empirical Comparison of Sorting Algorithms, CS201 OpenDSA (Duke).](https://opendsa-server.cs.vt.edu/ODSA/Books/duke/cs201/spring-2025/MW/html/SortingEmpirical.html)
18. [Robert Cypher (1993). A Lower Bound on the Size of Shellsort Sorting Networks. SIAM Journal on Computing 22(1):62–71.](https://epubs.siam.org/doi/10.1137/0222006)
19. [Bo Liu (2026). A New Gap Sequence for Shellsort: RL-Driven Algorithm Discovery Beyond N^{4/3}. arXiv:2609.29881.](https://arxiv.org/abs/2609.29881)
20. [A new upper bound for Shellsort](https://collaborate.princeton.edu/en/publications/a-new-upper-bound-for-shellsort/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Sorting, searching, and selection › Comparison sorting algorithms

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/shellsort

English original: https://www.edgepedia.org/shellsort
