# 二分查找算法

二分查找是一种在有序数组中查找目标值的算法：它反复将目标值与中间元素比较，并舍弃不可能包含目标值的那一半数组。每次比较都会将剩余查找范围减半，因此运行时间随数组规模呈对数增长：O(log n)，其中 n 为元素个数；相比之下，在无序列表中进行线性扫描的复杂度为 O(n)。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup><sup> • </sup><sup>[2](https://ocw.mit.edu/courses/6-00-introduction-to-computer-science-and-programming-fall-2008/e1c8c4fcfc48f347033239c8a023403d_6-00F08-L10.pdf)</sup>

| 关键事实 | 详情 |
|---|---|
| 前提条件 | 数组必须按照比较所假设的顺序排好序；在未排序的数组上，算法可能在本应存在某元素时报告其不存在。<sup>[3](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)</sup> |
| 时间复杂度 | O(log n) 次比较；运行时间等于一个常数加上与 log n 成正比的项。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup> |
| 基本步骤 | 将目标值与中间元素比较；若中间元素较小，则将下界设为 middle + 1，否则将上界设为 middle − 1。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup> |
| 终止条件 | 当找到该元素，或下界超过上界（即活动范围为空）时，循环停止。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup> |
| 插入代价 | 定位插入点需要 O(log n)，但向数组中插入需要 O(n)，因为其后的元素必须移动；对数级的查找被线性级的插入所主导。<sup>[4](https://docs.python.org/3.11/library/bisect.html?highlight=sort)</sup> |

## 算法的工作原理

该算法维护两个边界——下界和上界——它们界定了数组中仍值得搜索的部分。每次迭代时，它计算中间索引，将该处的元素与目标值比较，并收窄边界。若中间元素等于目标值，则搜索返回该索引。若中间元素小于目标值，则中间位置及其之前的每个元素都太小，因此下界移至 middle + 1；否则上界移至 middle − 1。当找到该元素，或下界大于上界时，搜索结束。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup>

一种常见的表述使用半开区间。指针初始为 left = 0 和 right = n，当 right > left 时循环运行，计算 mid 为 ⌊(left + right)/2⌋。若 L[mid] 等于目标值，则返回该索引；若 L[mid] 较大，则 right 变为 mid；若 L[mid] 较小，则 left 变为 mid + 1。循环退出时，算法返回 NULL。<sup>[5](https://esp.mit.edu/download/1c293235-4214-416d-9c9b-67072dde8363/C15664_HSSP_Algorithms_Course_Notes.pdf)</sup>

**为什么它是正确的。** 正确性论证依赖于一个循环不变量：若目标值 x 在列表中，则它必定位于活动子列表 L[left], ..., L[right − 1] 中。该不变量在初始时成立，此时活动子列表为整个列表；三种比较情形中的每一种都保持该不变量，因为有序性保证被舍弃的元素要么全都太小，要么全都太大。当循环以 right ≤ left 停止时，活动子列表为空，因此不变量表明 x 不在列表中，返回 NULL 是正确的。终止性成立，因为 right − left 在每次迭代中至少减少一半，因而至少减少 1。<sup>[5](https://esp.mit.edu/download/1c293235-4214-416d-9c9b-67072dde8363/C15664_HSSP_Algorithms_Course_Notes.pdf)</sup>

**为什么它是快速的。** 运行时间等于一次性完成的常数工作量加上每次迭代完成一次的与 log n 成正比的工作量，即 T(n) = c′ + c″ log n，也就是 O(log n)。<sup>[1](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)</sup> 正是对数的存在使得对集合排序能够收回成本：一旦数组排好序，每次搜索只需花费 log n 时间，而不是无序列表所需的线性遍历。<sup>[2](https://ocw.mit.edu/courses/6-00-introduction-to-computer-science-and-programming-fall-2008/e1c8c4fcfc48f347033239c8a023403d_6-00F08-L10.pdf)</sup> 排序本身使用快速排序之类的算法需要 O(n log n)，而插入排序则为 O(n²)。<sup>[6](https://ocw.mit.edu/courses/6-087-practical-programming-in-c-january-iap-2010/1aa34decd30f336826cd087d2f52035b_MIT6_087IAP10_lec06.pdf)</sup>

## 有序数组这一前提条件

有序性是数据结构的表示不变量：每个元素都必须小于其后的任何元素。只要该不变量成立，查询就返回正确结果；若不成立，二分查找可能在本应存在某元素时报告其不存在。<sup>[3](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)</sup> 排序还带来了其他常数时间的查询：最小值位于数组开头，最大值位于末尾。<sup>[3](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)</sup>

更新是薄弱环节。插入一个元素意味着找到它应处的位置，并将其后的所有元素后移以腾出空间，从而保持数组有序；在末尾追加会更快，但会破坏该不变量。<sup>[3](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)</sup> 这就是为什么二分查找适合基本静态的数据，而平衡二叉搜索树——其搜索过程类似于数组上的二分查找——支持 O(h) 时间的插入和删除，当树平衡时为 O(log n)。<sup>[3](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)</sup>

## 变体与库实现

**插入点查询。** 二分查找的许多用途不是询问某个值是否存在，而是询问它应插入何处。Python 的 bisect 模块将此实现为数组二分：bisect_left 和 bisect_right 返回最左和最右的插入点，而 insort 系列函数在插入的同时保持排序顺序。Python 3.10 新增的 key 参数使这些函数能够按[计算](https://www.edgepedia.org/zh/computing)出的键搜索复杂记录。文档指出，若要定位特定值，字典性能更佳；并且 insort 系列函数为 O(n)，因为对数级的查找步骤被线性级的插入所主导。<sup>[4](https://docs.python.org/3.11/library/bisect.html?highlight=sort)</sup>

**C++ 标准库。** C++ 的 <algorithm> 头文件提供了 binary_search、lower_bound、upper_bound 和 equal_range，它们都要求范围按照调用所给出的同一顺序排好序。对于随机访问迭代器，其复杂度为对数级，否则为线性级，步数与 last − first 成正比；这些算法不修改范围。<sup>[7](https://learn.microsoft.com/en-us/cpp/standard-library/algorithm-functions?view=msvc-170)</sup>

**一个经典的实现缺陷。** 将中点计算为 (low + high) / 2 时，若索引之和超过整数类型的最大值，就可能发生溢出。

## 超越有序数组

二分查找是分而治之的标准范例：每一步都求解一个规模减半的问题。<sup>[2](https://ocw.mit.edu/courses/6-00-introduction-to-computer-science-and-programming-fall-2008/e1c8c4fcfc48f347033239c8a023403d_6-00F08-L10.pdf)</sup>

近期研究将有序数组本身视为模型可以学习的对象。在学习型索引框架中，B 树索引被视为一个将键映射到记录在有序数组中位置的模型，而学习到的模型可以直接预测该位置；初步结果显示，在若干真实世界数据集上，神经网络的速度比缓存优化的 B 树快达 70%，同时节省一个数量级的内存。<sup>[8](https://research.google/pubs/the-case-for-learned-index-structures/)</sup> 关于带分布预测的二分查找的理论工作给出了一种查询复杂度为 O(H(p) + log η) 的算法，其中 H(p) 是目标值真实分布的熵，η 是该分布与预测分布之间的推土机距离，因此好的预测能将探测次数降到最坏情况 log n 以下。<sup>[9](https://arxiv.org/abs/2411.16030)</sup>

## References

1. [Lecture C10, The Binary Search Algorithm, MIT 16.01 Unified Engineering](https://live.ocw.mit.edu/courses/16-01-unified-engineering-i-ii-iii-iv-fall-2005-spring-2006/3c694a38ef571999a35fa42c97f3aab0_m10.pdf)
2. [Lecture 10 transcript, 6.00 Introduction to Computer Science and Programming, Fall 2008, MIT OpenCourseWare](https://ocw.mit.edu/courses/6-00-introduction-to-computer-science-and-programming-fall-2008/e1c8c4fcfc48f347033239c8a023403d_6-00F08-L10.pdf)
3. [Recitation 5: Recursion Trees, Binary Search Trees, 6.006 Introduction to Algorithms, MIT OpenCourseWare](https://mitocw.ups.edu.ec/courses/electrical-engineering-and-computer-science/6-006-introduction-to-algorithms-fall-2011/recitation-videos/recitation-5-recursion-trees-binary-search-trees/)
4. [bisect — Array bisection algorithm, Python 3.11 documentation](https://docs.python.org/3.11/library/bisect.html?highlight=sort)
5. [Algorithm 2: Binary Search, MIT ESP HSSP Algorithms Course Notes](https://esp.mit.edu/download/1c293235-4214-416d-9c9b-67072dde8363/C15664_HSSP_Algorithms_Course_Notes.pdf)
6. [Lecture 6: Searching and Sorting, 6.087 Practical Programming in C, MIT OpenCourseWare](https://ocw.mit.edu/courses/6-087-practical-programming-in-c-january-iap-2010/1aa34decd30f336826cd087d2f52035b_MIT6_087IAP10_lec06.pdf)
7. [<algorithm> functions, Microsoft Learn C++ standard library reference](https://learn.microsoft.com/en-us/cpp/standard-library/algorithm-functions?view=msvc-170)
8. [The Case for Learned Index Structures, Kraska et al., SIGMOD 2018, Google Research](https://research.google/pubs/the-case-for-learned-index-structures/)
9. [Binary Search with Distributional Predictions, arXiv](https://arxiv.org/abs/2411.16030)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: 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/binary-search-algorithm

English original: https://www.edgepedia.org/binary-search-algorithm
