// 头文件,包含所有
#include <bits/stdc++.h>
// 命名空间
using namespace std;
// 主函数
int main(){
// 定义一个数组
int a[7] = {180,170,176,160,155,150,140};
// 求数组长度
int length = sizeof(a)/sizeof(int);
// 循环列表,循环多少次
for(int i = 0;i<length - 1;i++){
// j < length-i - 1 此处是遍历数组,
//length-i 的意思是循环过的数字不在参与循环
for(int j = 0;j<length-i-1;j++){
// 如果右边的数字大于左边到达数字,加一
if(a[j] > a[j+1]){
// 交换两个数的位置,可以写为函数
int temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
// 打印列表数据
for(int i = 0;i<length;i++){
cout << a[i] << " ";
}
return 0;
}
c++ 冒泡排序
最新推荐文章于 2024-09-13 17:43:09 发布