# 冒泡排序

冒泡排序是一种简单的排序算法，它反复遍历列表，比较相邻元素，并在顺序错误时交换它们，使得每一轮遍历后较大的值逐步移向列表末尾。它是稳定的，即保持相等元素的相对顺序；它也是原地排序，除输入本身之外几乎不需要额外内存。<sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup><sup> • </sup><sup>[2](https://www.mycplus.com/computer-science/algorithms/bubble-sort/)</sup>

| 关键事实 | 详情 |
|---|---|
| 方法 | 反复遍历数组，交换顺序错误的相邻元素；第 i 轮之后，i 个最大元素已就位于最终位置<sup>[3](https://math.oxford.emory.edu/site/cs171/bubbleSort/)</sup> |
| 最坏情况 | O(n²) 次比较和交换：对逆序输入进行 n(n−1)/2 次比较<sup>[3](https://math.oxford.emory.edu/site/cs171/bubbleSort/)</sup> |
| 最好情况 | 带提前退出标志时为 O(n)：对已排序输入进行一趟 n−1 次比较，零次交换即停止<sup>[4](https://ucoruh.github.io/ce205-data-structures/week-10/cen207-week-10/)</sup> |
| 交换次数 | 每种变体的交换次数恰好等于输入中的逆序对数量，因为每次相邻交换消除一个逆序对<sup>[2](https://www.mycplus.com/computer-science/algorithms/bubble-sort/)</sup> |
| 性质 | 稳定且原地；除输入向量外几乎不需要额外空间<sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup> |
| 评价 | 高德纳（Donald Knuth）写道，它“似乎没有什么可取之处，除了一个朗朗上口的名字，以及它引出了一些有趣的理论问题”<sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup> |
| 实际作用 | 主要作为教学工具；插入排序几乎总是更好的替代方案，且代码复杂度增加极小<sup>[5](https://www.engineeringniche.com/article/comparing-bubble-sort-and-insertion-sort-which-is-more-efficient/)</sup> |

## 算法工作原理

一趟遍历从左到右扫过数组，比较每一对相邻元素，若左边较大则交换。第一趟之后，最大元素必然已到达数组末尾，因此第二趟无需与最后一个位置比较，第三趟可以提前两个位置停止，依此类推。对于 n 个元素的数组，最多需要 n−1 趟，因为一旦最后 n−1 个元素就位，第一个元素也必然就位。<sup>[3](https://math.oxford.emory.edu/site/cs171/bubbleSort/)</sup>

**提前退出。** 如果某一趟遍历没有进行任何交换，说明数组已经有序，算法可以停止。这一简单检查使算法具有自适应性：对已排序数组，它恰好执行 n−1 次比较后终止，表现为 O(n) 行为，其代价随无序程度而变化，而非总是付出完整的二次代价。<sup>[4](https://ucoruh.github.io/ce205-data-structures/week-10/cen207-week-10/)</sup><sup> • </sup><sup>[6](https://leon-lourenco.github.io/algorithms-project/sorting/bubble-sort/index.html)</sup>

**逆序对。** 逆序对是指相对于排序结果顺序错误的一对元素。每次相邻交换恰好消除一个逆序对，因此任何冒泡排序执行的总交换次数等于输入中的逆序对数量。最坏情况下，即逆序数组，为 n(n−1)/2 次交换；最好情况下为零。<sup>[2](https://www.mycplus.com/computer-science/algorithms/bubble-sort/)</sup>

## 性能

最坏情况下的比较次数为 (n−1) + (n−2) + ... + 1 = n(n−1)/2，即 O(n²)。对逆序输入的交换次数相同，平均情况约需 n(n−1)/4 次交换，仍为 O(n²)。<sup>[3](https://math.oxford.emory.edu/site/cs171/bubbleSort/)</sup> 实证比较证实了这一点：在一项随机数据研究中，冒泡排序在三种基本二次排序中耗时最长，而选择排序的平均时间最快；在接近有序的数据上，冒泡排序同样最慢，插入排序最快。<sup>[7](https://openjournal.unpam.ac.id/index.php/informatika/article/view/38800)</sup>

内层循环存在顺序依赖，因为每次交换必须在下一次比较开始前完成，这使得冒泡排序难以并行化。<sup>[8](https://masteringalgorithms.com/chapters/bubble-sort.html)</sup>

## 与插入排序和选择排序的比较

冒泡排序属于简单的 O(n²) 算法家族，该家族还包括插入排序和选择排序。其中，插入排序几乎总是表现出最好的实际运行时间，许多标准库对小规模或接近有序的输入改用插入排序，因为其开销低且具有自适应性。<sup>[9](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)</sup>

冒泡排序可以接受的应用场景包括：教学演示；大约十个元素或更少的极小数据集，此时性能差异可忽略不计；以及诸如脉动阵列之类的硬件实现，其中相邻交换可以并行执行。即使在这些情况下，插入排序几乎总是更好的替代方案。<sup>[5](https://www.engineeringniche.com/article/comparing-bubble-sort-and-insertion-sort-which-is-more-efficient/)</sup>

## 变体

**鸡尾酒排序**（双向冒泡排序）交替改变每趟遍历的方向。其最坏情况运行时间仍为 O(n²)；然而，当数组除一个远离其正确位置的元素——通常称为“乌龟”——之外均有序时，该算法可能比标准冒泡排序快数倍。<sup>[8](https://masteringalgorithms.com/chapters/bubble-sort.html)</sup>

冒泡排序操作也出现在简单排序之外的理论工作中。在一种动态数据模型中，每次比较后，对手随机交换正确排序序列中两个相邻元素，实验结果表明，当交换率保持恒定时，包括插入排序和冒泡排序在内的二次时间算法无法在渐近意义上被超越。<sup>[10](https://arxiv.org/abs/1805.05443)</sup>

## 历史

该算法的历史远早于其名称。斯坦福大学计算机科学家、《计算机程序设计艺术》（*The Art of Computer Programming*）的作者[高德纳](https://www.edgepedia.org/zh/donald-knuth)引用了 1956 年的一篇算法分析——爱德华·H·弗兰德（Edward H. Friend）发表于《ACM 杂志》（*Journal of the ACM*）的《电子计算机系统上的排序》（"Sorting on Electronic Computer Systems"），但该论文使用的是“交换排序”一词，而非冒泡排序。<sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup><sup> • </sup><sup>[11](https://dblp.dagstuhl.de/rec/journals/jacm/Friend56.html)</sup> 1959 年的一本编程书籍和 1962 年 ACM 排序会议的文章同样使用交换排序的说法，未使用冒泡排序之名。<sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup>

已知最早在印刷品中使用该术语的似乎是肯尼斯·艾弗森（Kenneth Iverson）于 1962 年的著作，而艾弗森引用的每一部作品都用其他说法描述该算法。杜克大学的欧文·阿斯特拉罕（Owen Astrachan）在《冒泡排序：一项考古学算法分析》（"Bubble Sort: An Archaeological Algorithmic Analysis"）中追溯了这段历史，他指出，尽管该算法遭到广泛嘲弄，也有教学论者主张将其排除在课程之外，但它仍在教科书中存续，部分原因在于历史的惯性和它朗朗上口的名字。<sup>[12](https://courses.cs.duke.edu/fall03/cps182s/latex/bubble182.pdf)</sup><sup> • </sup><sup>[1](https://users.cs.duke.edu/~ola/bubble/bubble.html)</sup>

## References

1. [Owen Astrachan. Bubble Sort: An Archaeological Algorithmic Analysis, Duke University.](https://users.cs.duke.edu/~ola/bubble/bubble.html)
2. [Bubble Sort in C and C++: Code, Passes and Complexity, MyCplus.](https://www.mycplus.com/computer-science/algorithms/bubble-sort/)
3. [Bubble Sort, Emory University CS171.](https://math.oxford.emory.edu/site/cs171/bubbleSort/)
4. [Week-10 Sorting, CEN207 Data Structures Course Notes.](https://ucoruh.github.io/ce205-data-structures/week-10/cen207-week-10/)
5. [Comparing Bubble Sort and Insertion Sort: Which Is More Efficient? Engineering Niche.](https://www.engineeringniche.com/article/comparing-bubble-sort-and-insertion-sort-which-is-more-efficient/)
6. [Bubble Sort, The Algorithm Almanac.](https://leon-lourenco.github.io/algorithms-project/sorting/bubble-sort/index.html)
7. [Analisa Perbandingan Algoritma Bubble Sort, Insert Sort dan Selection Sort, Jurnal Informatika Universitas Pamulang.](https://openjournal.unpam.ac.id/index.php/informatika/article/view/38800)
8. [Bubble Sort, Mastering Algorithms.](https://masteringalgorithms.com/chapters/bubble-sort.html)
9. [Sorting Algorithms, CS 2110, Cornell University.](https://courses.cis.cornell.edu/courses/cs2110/2026sp/lectures/lec07/)
10. [Quadratic Time Algorithms Appear to be Optimal for Sorting Evolving Data, arXiv:1805.05443.](https://arxiv.org/abs/1805.05443)
11. [Edward H. Friend (1956). Sorting on Electronic Computer Systems, J. ACM 3(3): 134-168, dblp record.](https://dblp.dagstuhl.de/rec/journals/jacm/Friend56.html)
12. [Owen Astrachan. Bubble Sort: An Archaeological Algorithmic Analysis (PDF), Duke University.](https://courses.cs.duke.edu/fall03/cps182s/latex/bubble182.pdf)

---
*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 › Comparison sorting algorithms

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bubble-sort

English original: https://www.edgepedia.org/bubble-sort
