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