上传文件至「260202」
This commit is contained in:
27
260202/冒泡排序.cpp
Normal file
27
260202/冒泡排序.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int a[5] = {8,114,24,88,5}; // 定义一个简单的数组
|
||||
int n = 5;
|
||||
|
||||
// 冒泡排序核心逻辑
|
||||
for (int i = 0; i < n - 1; i++) { // 外层循环:控制比较轮数
|
||||
for (int j = 0; j < n - 1 - i; j++) { // 内层循环:比较相邻两个数
|
||||
if (a[j] > a[j + 1]) { // 如果前面的比后面的大
|
||||
// 交换位置
|
||||
int temp = a[j];
|
||||
a[j] = a[j + 1];
|
||||
a[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 输出结果
|
||||
cout << "排序结果: ";
|
||||
for (int i = 0; i < n; i++) {
|
||||
cout << a[i] << " ";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user