2023-02-08 15:20:18 +08:00
|
|
|
|
---
|
|
|
|
|
comments: true
|
|
|
|
|
---
|
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
# 11.3 冒泡排序
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-20 23:27:50 +08:00
|
|
|
|
「冒泡排序 bubble sort」通过连续地比较与交换相邻元素实现排序。这个过程就像气泡从底部升到顶部一样,因此得名冒泡排序。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
如图 11-4 所示,冒泡过程可以利用元素交换操作来模拟:从数组最左端开始向右遍历,依次比较相邻元素大小,如果“左元素 > 右元素”就交换它俩。遍历完成后,最大的元素会被移动到数组的最右端。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<1>"
|
2023-05-24 00:32:38 +08:00
|
|
|
|
![利用元素交换操作模拟冒泡](bubble_sort.assets/bubble_operation_step1.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<2>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step2](bubble_sort.assets/bubble_operation_step2.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<3>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step3](bubble_sort.assets/bubble_operation_step3.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<4>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step4](bubble_sort.assets/bubble_operation_step4.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<5>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step5](bubble_sort.assets/bubble_operation_step5.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<6>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step6](bubble_sort.assets/bubble_operation_step6.png)
|
|
|
|
|
|
2023-02-22 00:57:04 +08:00
|
|
|
|
=== "<7>"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
![bubble_operation_step7](bubble_sort.assets/bubble_operation_step7.png)
|
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 11-4 利用元素交换操作模拟冒泡 </p>
|
2023-08-17 05:12:05 +08:00
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
## 11.3.1 算法流程
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
设数组的长度为 $n$ ,冒泡排序的步骤如图 11-5 所示。
|
2023-03-25 18:41:53 +08:00
|
|
|
|
|
2023-05-24 00:32:38 +08:00
|
|
|
|
1. 首先,对 $n$ 个元素执行“冒泡”,**将数组的最大元素交换至正确位置**,
|
|
|
|
|
2. 接下来,对剩余 $n - 1$ 个元素执行“冒泡”,**将第二大元素交换至正确位置**。
|
|
|
|
|
3. 以此类推,经过 $n - 1$ 轮“冒泡”后,**前 $n - 1$ 大的元素都被交换至正确位置**。
|
2023-08-20 14:52:31 +08:00
|
|
|
|
4. 仅剩的一个元素必定是最小元素,无须排序,因此数组排序完成。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-02-26 18:17:57 +08:00
|
|
|
|
![冒泡排序流程](bubble_sort.assets/bubble_sort_overview.png)
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-08-22 13:50:12 +08:00
|
|
|
|
<p align="center"> 图 11-5 冒泡排序流程 </p>
|
2023-02-26 19:53:26 +08:00
|
|
|
|
|
2023-02-08 15:20:18 +08:00
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="bubble_sort.java"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
void bubbleSort(int[] nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int i = nums.length - 1; i > 0; i--) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="bubble_sort.cpp"
|
|
|
|
|
/* 冒泡排序 */
|
2023-04-14 04:01:38 +08:00
|
|
|
|
void bubbleSort(vector<int> &nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int i = nums.size() - 1; i > 0; i--) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
// 这里使用了 std::swap() 函数
|
|
|
|
|
swap(nums[j], nums[j + 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="bubble_sort.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def bubble_sort(nums: list[int]):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""冒泡排序"""
|
2023-05-22 22:01:18 +08:00
|
|
|
|
n = len(nums)
|
2023-05-24 00:32:38 +08:00
|
|
|
|
# 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i in range(n - 1, 0, -1):
|
2023-05-24 00:32:38 +08:00
|
|
|
|
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for j in range(i):
|
|
|
|
|
if nums[j] > nums[j + 1]:
|
|
|
|
|
# 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
nums[j], nums[j + 1] = nums[j + 1], nums[j]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="bubble_sort.go"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
func bubbleSort(nums []int) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i := len(nums) - 1; i > 0; i-- {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
if nums[j] > nums[j+1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
nums[j], nums[j+1] = nums[j+1], nums[j]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="bubble_sort.js"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
function bubbleSort(nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let i = nums.length - 1; i > 0; i--) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="bubble_sort.ts"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
function bubbleSort(nums: number[]): void {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let i = nums.length - 1; i > 0; i--) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="bubble_sort.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
void bubbleSort(int nums[], int size) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-04-18 20:19:07 +08:00
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-04-18 20:19:07 +08:00
|
|
|
|
for (int j = 0; j < size - 1 - i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
int temp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="bubble_sort.cs"
|
|
|
|
|
/* 冒泡排序 */
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void bubbleSort(int[] nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = nums.Length - 1; i > 0; i--) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="bubble_sort.swift"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
func bubbleSort(nums: inout [Int]) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i in stride(from: nums.count - 1, to: 0, by: -1) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for j in stride(from: 0, to: i, by: 1) {
|
|
|
|
|
if nums[j] > nums[j + 1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j]
|
|
|
|
|
nums[j] = nums[j + 1]
|
|
|
|
|
nums[j + 1] = tmp
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="bubble_sort.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 冒泡排序
|
|
|
|
|
fn bubbleSort(nums: []i32) void {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var i: usize = nums.len - 1;
|
|
|
|
|
while (i > 0) : (i -= 1) {
|
|
|
|
|
var j: usize = 0;
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (j < i) : (j += 1) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
var tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="bubble_sort.dart"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
void bubbleSort(List<int> nums) {
|
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
|
|
|
|
for (int i = nums.length - 1; i > 0; i--) {
|
2023-08-13 19:36:03 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-06-02 02:38:24 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="bubble_sort.rs"
|
|
|
|
|
/* 冒泡排序 */
|
|
|
|
|
fn bubble_sort(nums: &mut [i32]) {
|
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
|
|
|
|
for i in (1..nums.len()).rev() {
|
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
|
|
|
|
for j in 0..i {
|
|
|
|
|
if nums[j] > nums[j + 1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
## 11.3.2 效率优化
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-05-24 00:32:38 +08:00
|
|
|
|
我们发现,如果某轮“冒泡”中没有执行任何交换操作,说明数组已经完成排序,可直接返回结果。因此,可以增加一个标志位 `flag` 来监测这种情况,一旦出现就立即返回。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
2023-04-10 03:12:02 +08:00
|
|
|
|
经过优化,冒泡排序的最差和平均时间复杂度仍为 $O(n^2)$ ;但当输入数组完全有序时,可达到最佳时间复杂度 $O(n)$ 。
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
=== "Java"
|
|
|
|
|
|
|
|
|
|
```java title="bubble_sort.java"
|
2023-04-14 04:01:38 +08:00
|
|
|
|
/* 冒泡排序(标志优化) */
|
2023-02-08 15:20:18 +08:00
|
|
|
|
void bubbleSortWithFlag(int[] nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int i = nums.length - 1; i > 0; i--) {
|
|
|
|
|
boolean flag = false; // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
2023-04-14 04:01:38 +08:00
|
|
|
|
flag = true; // 记录交换元素
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (!flag)
|
|
|
|
|
break; // 此轮冒泡未交换任何元素,直接跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C++"
|
|
|
|
|
|
|
|
|
|
```cpp title="bubble_sort.cpp"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
2023-04-14 04:01:38 +08:00
|
|
|
|
void bubbleSortWithFlag(vector<int> &nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int i = nums.size() - 1; i > 0; i--) {
|
|
|
|
|
bool flag = false; // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
// 这里使用了 std::swap() 函数
|
|
|
|
|
swap(nums[j], nums[j + 1]);
|
2023-04-14 04:01:38 +08:00
|
|
|
|
flag = true; // 记录交换元素
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-14 04:01:38 +08:00
|
|
|
|
if (!flag)
|
|
|
|
|
break; // 此轮冒泡未交换任何元素,直接跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
```python title="bubble_sort.py"
|
2023-07-25 16:42:55 +08:00
|
|
|
|
def bubble_sort_with_flag(nums: list[int]):
|
2023-04-09 05:12:22 +08:00
|
|
|
|
"""冒泡排序(标志优化)"""
|
2023-05-22 22:01:18 +08:00
|
|
|
|
n = len(nums)
|
2023-05-24 00:32:38 +08:00
|
|
|
|
# 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i in range(n - 1, 0, -1):
|
2023-05-22 22:01:18 +08:00
|
|
|
|
flag = False # 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
# 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for j in range(i):
|
|
|
|
|
if nums[j] > nums[j + 1]:
|
|
|
|
|
# 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
nums[j], nums[j + 1] = nums[j + 1], nums[j]
|
|
|
|
|
flag = True # 记录交换元素
|
|
|
|
|
if not flag:
|
2023-04-09 05:12:22 +08:00
|
|
|
|
break # 此轮冒泡未交换任何元素,直接跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
```go title="bubble_sort.go"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
func bubbleSortWithFlag(nums []int) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i := len(nums) - 1; i > 0; i-- {
|
|
|
|
|
flag := false // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for j := 0; j < i; j++ {
|
|
|
|
|
if nums[j] > nums[j+1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
nums[j], nums[j+1] = nums[j+1], nums[j]
|
|
|
|
|
flag = true // 记录交换元素
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if flag == false { // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "JS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```javascript title="bubble_sort.js"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
function bubbleSortWithFlag(nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let i = nums.length - 1; i > 0; i--) {
|
|
|
|
|
let flag = false; // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
2023-04-17 21:57:42 +08:00
|
|
|
|
flag = true; // 记录交换元素
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-17 21:57:42 +08:00
|
|
|
|
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 15:34:46 +08:00
|
|
|
|
=== "TS"
|
2023-02-08 15:20:18 +08:00
|
|
|
|
|
|
|
|
|
```typescript title="bubble_sort.ts"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
function bubbleSortWithFlag(nums: number[]): void {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let i = nums.length - 1; i > 0; i--) {
|
|
|
|
|
let flag = false; // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for (let j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
2023-02-08 16:47:52 +08:00
|
|
|
|
flag = true; // 记录交换元素
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 16:47:52 +08:00
|
|
|
|
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
2023-02-08 15:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C"
|
|
|
|
|
|
|
|
|
|
```c title="bubble_sort.c"
|
2023-04-18 20:19:07 +08:00
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
void bubbleSortWithFlag(int nums[], int size) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-04-18 20:19:07 +08:00
|
|
|
|
for (int i = 0; i < size - 1; i++) {
|
|
|
|
|
bool flag = false;
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-04-18 20:19:07 +08:00
|
|
|
|
for (int j = 0; j < size - 1 - i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
int temp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = temp;
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!flag)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
```csharp title="bubble_sort.cs"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
2023-04-23 14:58:03 +08:00
|
|
|
|
void bubbleSortWithFlag(int[] nums) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int i = nums.Length - 1; i > 0; i--) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
bool flag = false; // 初始化标志位
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-04-23 14:58:03 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
2023-02-08 15:20:18 +08:00
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
flag = true; // 记录交换元素
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Swift"
|
|
|
|
|
|
|
|
|
|
```swift title="bubble_sort.swift"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
func bubbleSortWithFlag(nums: inout [Int]) {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-08 15:20:18 +08:00
|
|
|
|
for i in stride(from: nums.count - 1, to: 0, by: -1) {
|
|
|
|
|
var flag = false // 初始化标志位
|
|
|
|
|
for j in stride(from: 0, to: i, by: 1) {
|
|
|
|
|
if nums[j] > nums[j + 1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j]
|
|
|
|
|
nums[j] = nums[j + 1]
|
|
|
|
|
nums[j + 1] = tmp
|
|
|
|
|
flag = true // 记录交换元素
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !flag { // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
```zig title="bubble_sort.zig"
|
2023-02-09 22:55:29 +08:00
|
|
|
|
// 冒泡排序(标志优化)
|
|
|
|
|
fn bubbleSortWithFlag(nums: []i32) void {
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
2023-02-09 22:55:29 +08:00
|
|
|
|
var i: usize = nums.len - 1;
|
|
|
|
|
while (i > 0) : (i -= 1) {
|
|
|
|
|
var flag = false; // 初始化标志位
|
|
|
|
|
var j: usize = 0;
|
2023-05-24 00:32:38 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-02-09 22:55:29 +08:00
|
|
|
|
while (j < i) : (j += 1) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
var tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-08 15:20:18 +08:00
|
|
|
|
```
|
2023-05-24 00:32:38 +08:00
|
|
|
|
|
2023-06-02 02:38:24 +08:00
|
|
|
|
=== "Dart"
|
|
|
|
|
|
|
|
|
|
```dart title="bubble_sort.dart"
|
|
|
|
|
/* 冒泡排序(标志优化)*/
|
|
|
|
|
void bubbleSortWithFlag(List<int> nums) {
|
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
|
|
|
|
for (int i = nums.length - 1; i > 0; i--) {
|
|
|
|
|
bool flag = false; // 初始化标志位
|
2023-08-13 19:36:03 +08:00
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
2023-06-02 02:38:24 +08:00
|
|
|
|
for (int j = 0; j < i; j++) {
|
|
|
|
|
if (nums[j] > nums[j + 1]) {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
int tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
flag = true; // 记录交换元素
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-07-26 10:57:40 +08:00
|
|
|
|
=== "Rust"
|
|
|
|
|
|
|
|
|
|
```rust title="bubble_sort.rs"
|
|
|
|
|
/* 冒泡排序(标志优化) */
|
|
|
|
|
fn bubble_sort_with_flag(nums: &mut [i32]) {
|
|
|
|
|
// 外循环:未排序区间为 [0, i]
|
|
|
|
|
for i in (1..nums.len()).rev() {
|
|
|
|
|
let mut flag = false; // 初始化标志位
|
|
|
|
|
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
|
|
|
|
for j in 0..i {
|
|
|
|
|
if nums[j] > nums[j + 1] {
|
|
|
|
|
// 交换 nums[j] 与 nums[j + 1]
|
|
|
|
|
let tmp = nums[j];
|
|
|
|
|
nums[j] = nums[j + 1];
|
|
|
|
|
nums[j + 1] = tmp;
|
|
|
|
|
flag = true; // 记录交换元素
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !flag {break}; // 此轮冒泡未交换任何元素,直接跳出
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2023-08-19 22:07:27 +08:00
|
|
|
|
## 11.3.3 算法特性
|
2023-05-24 00:32:38 +08:00
|
|
|
|
|
2023-08-21 03:56:41 +08:00
|
|
|
|
- **时间复杂度为 $O(n^2)$ 、自适应排序** :各轮“冒泡”遍历的数组长度依次为 $n - 1$ , $n - 2$ , $\dots$ , $2$ , $1$ ,总和为 $(n - 1) n / 2$ 。在引入 `flag` 优化后,最佳时间复杂度可达到 $O(n)$ 。
|
2023-05-24 00:32:38 +08:00
|
|
|
|
- **空间复杂度为 $O(1)$ 、原地排序**:指针 $i$ , $j$ 使用常数大小的额外空间。
|
|
|
|
|
- **稳定排序**:由于在“冒泡”中遇到相等元素不交换。
|