# 指数搜索

**指数搜索**是一种用于有序数组的搜索算法，它先通过探测索引1、2、4、8等位置找到一个包含目标的有界区间，然后在该区间内运行二分查找。它也被称为**倍增搜索**或**飞奔搜索**，专为大小未知或无界的有序数组而设计。<sup>[1](https://pkg.go.dev/github.com/thuva4/algorithms/algorithms/searching/exponential-search)</sup> 该算法归功于乔恩·本特利（Jon Bentley）和安德鲁·姚（Andrew Yao），他们于1976年在《信息处理快报》（*Information Processing Letters*）上以《一种近乎最优的无界搜索算法》为题发表了该算法。<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup>

| 关键事实 | 详情 |
|---|---|
| 其他名称 | 倍增搜索、飞奔搜索；三者均指同一“先倍增后二分”的技术<sup>[3](https://unseel.com/cs/exponential-search)</sup> |
| 起源 | 乔恩·本特利与安德鲁·姚，《一种近乎最优的无界搜索算法》，《信息处理快报》，1976年<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup> |
| 运行时间 | 当目标位于索引i处时为Θ(log i)，与总大小n无关，而n可能未知或无限<sup>[3](https://unseel.com/cs/exponential-search)</sup> |
| 最坏情况 | Θ(log n)次比较，与二分查找的渐近代价相同<sup>[4](https://lec.inf.ethz.ch/DA/2017/slides/daLecture3.en.handout.4x4.pdf)</sup> |
| 要求 | 数据有序；每步一次除法或移位，与二分查找相同<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup> |
| 最佳用途 | 无界或极大型有序集合，或目标可能靠近数组前端的情形<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup><sup> • </sup><sup>[3](https://unseel.com/cs/exponential-search)</sup> |
| 著名应用 | Timsort合并有序数据段时的飞奔模式<sup>[1](https://pkg.go.dev/github.com/thuva4/algorithms/algorithms/searching/exponential-search)</sup> |

## 算法工作原理

该算法分为两个阶段。第一阶段，它检查索引1处的元素，然后检查2、4、8，每次将偏移量翻倍，直到找到一个大于或等于目标的元素。这将目标夹在两个探测位置之间。第二阶段，它在该区间内运行普通的二分查找，以精确定位目标或确认其不存在。<sup>[3](https://unseel.com/cs/exponential-search)</sup> Timsort的变体在偏移量1、3、7、15、31处探测，然后切换到区间内的二分查找。<sup>[5](https://www.algorithms-data-structures.com/essays/when-galloping-pays/)</sup>

倍增阶段赋予了该算法名称和代价特征。如果目标位于索引i处，探测在大约log₂ i步后越过i，随后二分查找覆盖的区间大小至多约为i，因此总代价为Θ(log i)。这一上界与总大小n无关，当n未知或实际无限时这一点至关重要。<sup>[3](https://unseel.com/cs/exponential-search)</sup> 当数组长度n已知时，最坏情况为Θ(log n)次比较，与二分查找相当。<sup>[4](https://lec.inf.ethz.ch/DA/2017/slides/daLecture3.en.handout.4x4.pdf)</sup>

一种实用的改进是从已知偏移量p而非数组开头开始探测。由于目标必定位于位置p之后，搜索将位置p视为虚拟列表的起点，代价变为O(log i)，其中i是从p到目标位置的距离，无论目标是否存在。当前元素一旦超过期望值，搜索也可以立即终止，从而提前确认目标不存在。<sup>[6](https://repositorio.uchile.cl/bitstream/handle/2250/208969/topological-data-analysis-for-classification-of-noisy-and-high-dimensional-datasets.pdf?isAllowed=y&sequence=1)</sup>

## 无界搜索

最初的动机是搜索大小事先未知的数组。指数搜索使二分查找适应调用者不知道的数组大小：倍增阶段从不需要n，因为一旦探测越过目标就会停止。<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup> 该技术对无界数组尤为有用，即大小实际无限的数组。<sup>[7](https://www.mdpi.com/1999-4893/15/1/10)</sup> 它也适用于数组之外的无界集合，例如定义在自然数上的函数的有序值域。<sup>[8](https://www.baeldung.com/cs/exponential-search)</sup>

本特利和姚1976年的论文还引入了更精细的无界搜索变体，进一步削减了常数因子。<sup>[3](https://unseel.com/cs/exponential-search)</sup> 对于分布已知的无界情形，可以在区间内嵌入插值查找，在均匀数据上达到O(log log i)的期望时间，不过若分布具有对抗性，该变体有O(i)最坏情况时间的风险。<sup>[3](https://unseel.com/cs/exponential-search)</sup>

## 有序列表交集运算中的飞奔

一个主要应用是计算两个有序序列的交集。存在两种基本方法：归并，以O(d₁ + d₂)的代价同步遍历两个列表；以及飞奔，对较小列表的每个元素在较大列表上进行指数探测再二分查找，代价为O(d₁ log d₂)。<sup>[9](https://github.com/AviAvni/database-learning-path/blob/master/topics/13-graph-engines/reading-wcoj.md)</sup> 飞奔交集搜索以这种方式将线性扫描与指数跳跃结合起来。<sup>[10](https://tamnd.github.io/brain/wiki/algorithms/01/12/349/)</sup> 当一个列表明显短于另一个时，基于搜索的算法表现更好，因为最短列表中的元素充当枢轴，跳过长列表中不匹配的元素；没有哪一种交集算法在所有场景下都优于其他算法，这促使人们在它们之间进行基于代价的选择。<sup>[11](https://www.vldb.org/pvldb/vol12/p1-kim.pdf)</sup>

同样的思想延伸到硬件加速。数据科学研究教授丹尼尔·勒米尔（Daniel Lemire）与列昂尼德·博伊佐夫（Leonid Boytsov）和内森·库尔茨（Nathan Kurz）提出了SIMD飞奔算法，利用一条SIMD指令可以同时比较四对整数这一事实；在两个TREC文本集合上，他们用于合取查询的技术可以使最先进方法的速度翻倍。<sup>[12](https://arxiv.org/abs/1401.6399)</sup>

## 排序中的飞奔：Timsort

Timsort是Python和Java中的标准稳定排序，它使用基于指数搜索的飞奔模式来高效合并有序数据段。<sup>[1](https://pkg.go.dev/github.com/thuva4/algorithms/algorithms/searching/exponential-search)</sup> 在合并过程中，当一个段反复提供元素时，飞奔例程切换到指数探测，以确定在另一个段的下一个元素之前可以从该段复制多大的块，同时保持合并不变量：输出有序，且包含两个段中尚未合并的最小元素。<sup>[13](https://tamnd.github.io/brain/wiki/algorithms/01/09/268/)</sup>

比较次数的节省可以量化。在一个实测输入上，Timsort用了58,599次比较，而归并排序用了92,436次，文章将Timsort的全部优势归功于飞奔；pdqsort以另一种机制——一趟分区将相等块分离出去——以46,004次比较位居第一。在近乎有序的输入上，Timsort对8,192个元素花费了33,449次比较，约每个元素四次比较，这反映的是合并树的深度，而非飞奔所控制的因素。<sup>[5](https://www.algorithms-data-structures.com/essays/when-galloping-pays/)</sup>

理论分析既揭示了收益也揭示了局限。引入飞奔例程是为了让Timsort在含少量不同值的数组上更高效，它确实能让Timsort以线性时间排序二值数组，但并不能阻止Timsort在长度为n、含三个不同值的数组上需要多达Θ(n log n)次元素比较。对该例程稍作修改，对于含σ个不同值的数组，可在最坏情况下达到O(n + n log σ)次比较。<sup>[14](https://arxiv.org/abs/2012.03996)</sup>

## 与其他搜索的比较

在基于比较的有序数组搜索中，指数搜索与二分查找共享O(log n)的最坏情况，但当目标可能靠近前端或大小n未知时，将典型情况改善为O(log i)。<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup> [跳跃搜索](https://www.edgepedia.org/zh/jump-search)故意以劣于二分查找为代价换取简单性，插值查找在均匀分布数据上表现更好但在偏斜数据上退化为O(n)最坏情况，斐波那契查找仅使用加减法就达到与二分查找相同的渐近代价。<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup> 没有哪一种技术在每个维度上都优于其他所有技术。<sup>[2](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)</sup>

倍增策略也出现在搜索之外的领域：同样的倍增思想赋予动态数组摊还O(1)的追加操作，这正是log i搜索上界的镜像。<sup>[3](https://unseel.com/cs/exponential-search)</sup>

## References

1. [Exponential Search directory, thuva4/algorithms on Go Packages](https://pkg.go.dev/github.com/thuva4/algorithms/algorithms/searching/exponential-search)
2. [Week-6 (Search and Hashing), RTEU CEN207 Data Structures Course Notes](https://ucoruh.github.io/ce205-data-structures/week-6/cen207-week-6/)
3. [Exponential Search: Locating a Target in an Unbounded Sorted Array, Unseel](https://unseel.com/cs/exponential-search)
4. [The Search Problem, ETH Zurich lecture handout](https://lec.inf.ethz.ch/DA/2017/slides/daLecture3.en.handout.4x4.pdf)
5. [When galloping pays, Algorithms & Data Structures](https://www.algorithms-data-structures.com/essays/when-galloping-pays/)
6. [Topological Data Analysis for Classification of Noisy and High-Dimensional Datasets, University of Chile thesis](https://repositorio.uchile.cl/bitstream/handle/2250/208969/topological-data-analysis-for-classification-of-noisy-and-high-dimensional-datasets.pdf?isAllowed=y&sequence=1)
7. [Searching Monotone Arrays: A Survey, Algorithms (MDPI)](https://www.mdpi.com/1999-4893/15/1/10)
8. [Exponential Search, Baeldung on Computer Science](https://www.baeldung.com/cs/exponential-search)
9. [Worst-Case Optimal Join reading, database-learning-path (GitHub)](https://github.com/AviAvni/database-learning-path/blob/master/topics/13-graph-engines/reading-wcoj.md)
10. [Galloping Intersection Search, brain](https://tamnd.github.io/brain/wiki/algorithms/01/12/349/)
11. [List Intersection for Web Search: Algorithms, Cost Models, and Optimizations, PVLDB 12(1)](https://www.vldb.org/pvldb/vol12/p1-kim.pdf)
12. [SIMD Compression and the Intersection of Sorted Integers, arXiv:1401.6399](https://arxiv.org/abs/1401.6399)
13. [Galloping Merge, brain](https://tamnd.github.io/brain/wiki/algorithms/01/09/268/)
14. [Galloping in fast-growth natural merge sorts, arXiv:2012.03996](https://arxiv.org/abs/2012.03996)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Sorting, searching, and selection › Searching in ordered and unordered data

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/exponential-search

English original: https://www.edgepedia.org/exponential-search
