2023-02-16 03:39:01 +08:00
|
|
|
|
# 冒泡排序
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-08-20 23:28:30 +08:00
|
|
|
|
「冒泡排序 bubble sort」通过连续地比较与交换相邻元素实现排序。这个过程就像气泡从底部升到顶部一样,因此得名冒泡排序。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-05-23 21:20:14 +08:00
|
|
|
|
我们可以利用元素交换操作模拟上述过程:从数组最左端开始向右遍历,依次比较相邻元素大小,如果“左元素 > 右元素”就交换它俩。遍历完成后,最大的元素会被移动到数组的最右端。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<1>"
|
2023-05-23 21:20:14 +08:00
|
|
|
|
![利用元素交换操作模拟冒泡](bubble_sort.assets/bubble_operation_step1.png)
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<2>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step2](bubble_sort.assets/bubble_operation_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<3>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step3](bubble_sort.assets/bubble_operation_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<4>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step4](bubble_sort.assets/bubble_operation_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<5>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step5](bubble_sort.assets/bubble_operation_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<6>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step6](bubble_sort.assets/bubble_operation_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:43 +08:00
|
|
|
|
=== "<7>"
|
2022-11-22 17:47:26 +08:00
|
|
|
|
![bubble_operation_step7](bubble_sort.assets/bubble_operation_step7.png)
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 算法流程
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-05-24 20:57:08 +08:00
|
|
|
|
设数组的长度为 $n$ ,冒泡排序的步骤为:
|
2023-03-25 18:41:22 +08:00
|
|
|
|
|
2023-05-23 21:20:14 +08:00
|
|
|
|
1. 首先,对 $n$ 个元素执行“冒泡”,**将数组的最大元素交换至正确位置**,
|
|
|
|
|
2. 接下来,对剩余 $n - 1$ 个元素执行“冒泡”,**将第二大元素交换至正确位置**。
|
|
|
|
|
3. 以此类推,经过 $n - 1$ 轮“冒泡”后,**前 $n - 1$ 大的元素都被交换至正确位置**。
|
2023-08-20 14:51:39 +08:00
|
|
|
|
4. 仅剩的一个元素必定是最小元素,无须排序,因此数组排序完成。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-02-26 18:18:34 +08:00
|
|
|
|
![冒泡排序流程](bubble_sort.assets/bubble_sort_overview.png)
|
2022-11-23 15:50:59 +08:00
|
|
|
|
|
2022-11-22 17:47:26 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```java title="bubble_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{bubble_sort}-[func]{bubbleSort}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
2022-12-01 18:28:57 +08:00
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="bubble_sort.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```python title="bubble_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{bubble_sort}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-12-03 01:31:29 +08:00
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="bubble_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="bubble_sort.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="bubble_sort.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="bubble_sort.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="bubble_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{bubble_sort}-[func]{bubbleSort}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-01-08 19:41:05 +08:00
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="bubble_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-02-01 22:03:04 +08:00
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="bubble_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{bubbleSort}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="bubble_sort.dart"
|
|
|
|
|
[class]{}-[func]{bubbleSort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="bubble_sort.rs"
|
|
|
|
|
[class]{}-[func]{bubble_sort}
|
|
|
|
|
```
|
|
|
|
|
|
2023-02-16 03:39:01 +08:00
|
|
|
|
## 效率优化
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-05-23 21:20:14 +08:00
|
|
|
|
我们发现,如果某轮“冒泡”中没有执行任何交换操作,说明数组已经完成排序,可直接返回结果。因此,可以增加一个标志位 `flag` 来监测这种情况,一旦出现就立即返回。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
2023-04-10 03:11:49 +08:00
|
|
|
|
经过优化,冒泡排序的最差和平均时间复杂度仍为 $O(n^2)$ ;但当输入数组完全有序时,可达到最佳时间复杂度 $O(n)$ 。
|
2022-11-22 17:47:26 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```java title="bubble_sort.java"
|
2023-02-07 04:43:52 +08:00
|
|
|
|
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
|
2022-11-22 17:47:26 +08:00
|
|
|
|
```
|
2022-11-26 01:40:49 +08:00
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="bubble_sort.cpp"
|
2023-02-08 04:17:26 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```
|
|
|
|
|
|
2022-11-26 01:40:49 +08:00
|
|
|
|
=== "Python"
|
|
|
|
|
|
2022-11-27 04:20:30 +08:00
|
|
|
|
```python title="bubble_sort.py"
|
2023-02-06 23:23:21 +08:00
|
|
|
|
[class]{}-[func]{bubble_sort_with_flag}
|
2022-11-26 01:40:49 +08:00
|
|
|
|
```
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="bubble_sort.go"
|
2023-02-09 04:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "JS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
2023-02-08 04:27:55 +08:00
|
|
|
|
```javascript title="bubble_sort.js"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:35:38 +08:00
|
|
|
|
=== "TS"
|
2022-12-03 01:31:29 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="bubble_sort.ts"
|
2023-02-08 19:45:06 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="bubble_sort.c"
|
2023-02-11 18:22:27 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="bubble_sort.cs"
|
2023-02-08 22:18:02 +08:00
|
|
|
|
[class]{bubble_sort}-[func]{bubbleSortWithFlag}
|
2022-12-03 01:31:29 +08:00
|
|
|
|
```
|
2023-01-08 19:41:05 +08:00
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="bubble_sort.swift"
|
2023-02-08 20:30:05 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2023-01-08 19:41:05 +08:00
|
|
|
|
```
|
2023-02-01 22:03:04 +08:00
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="bubble_sort.zig"
|
2023-02-09 22:57:25 +08:00
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
2023-02-01 22:03:04 +08:00
|
|
|
|
```
|
2023-05-23 21:20:14 +08:00
|
|
|
|
|
2023-06-02 02:40:26 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="bubble_sort.dart"
|
|
|
|
|
[class]{}-[func]{bubbleSortWithFlag}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 11:00:53 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="bubble_sort.rs"
|
|
|
|
|
[class]{}-[func]{bubble_sort_with_flag}
|
|
|
|
|
```
|
|
|
|
|
|
2023-05-23 21:20:14 +08:00
|
|
|
|
## 算法特性
|
|
|
|
|
|
2023-08-21 03:06:53 +08:00
|
|
|
|
- **时间复杂度为 $O(n^2)$ 、自适应排序** :各轮“冒泡”遍历的数组长度依次为 $n - 1$ , $n - 2$ , $\cdots$ , $2$ , $1$ ,总和为 $(n - 1) n / 2$ 。在引入 `flag` 优化后,最佳时间复杂度可达到 $O(n)$ 。
|
2023-05-24 00:05:12 +08:00
|
|
|
|
- **空间复杂度为 $O(1)$ 、原地排序**:指针 $i$ , $j$ 使用常数大小的额外空间。
|
|
|
|
|
- **稳定排序**:由于在“冒泡”中遇到相等元素不交换。
|