This commit is contained in:
krahets
2023-08-19 22:07:27 +08:00
parent 71c7786f51
commit 2e27ad1680
99 changed files with 283 additions and 283 deletions
+4 -4
View File
@@ -2,7 +2,7 @@
comments: true
---
# 11.3.   冒泡排序
# 11.3   冒泡排序
「冒泡排序 Bubble Sort」通过连续地比较与交换相邻元素实现排序。这个过程就像气泡从底部升到顶部一样,因此得名冒泡排序。
@@ -31,7 +31,7 @@ comments: true
<p align="center"> 图:利用元素交换操作模拟冒泡 </p>
## 11.3.1. &nbsp; 算法流程
## 11.3.1 &nbsp; 算法流程
设数组的长度为 $n$ ,冒泡排序的步骤为:
@@ -277,7 +277,7 @@ comments: true
}
```
## 11.3.2. &nbsp; 效率优化
## 11.3.2 &nbsp; 效率优化
我们发现,如果某轮“冒泡”中没有执行任何交换操作,说明数组已经完成排序,可直接返回结果。因此,可以增加一个标志位 `flag` 来监测这种情况,一旦出现就立即返回。
@@ -559,7 +559,7 @@ comments: true
}
```
## 11.3.3. &nbsp; 算法特性
## 11.3.3 &nbsp; 算法特性
- **时间复杂度为 $O(n^2)$ 、自适应排序** :各轮“冒泡”遍历的数组长度依次为 $n - 1$ , $n - 2$ , $\cdots$ , $2$ , $1$ ,总和为 $\frac{(n - 1) n}{2}$ 。在引入 `flag` 优化后,最佳时间复杂度可达到 $O(n)$ 。
- **空间复杂度为 $O(1)$ 、原地排序**:指针 $i$ , $j$ 使用常数大小的额外空间。