Files
python/TangDou/LuoGuBook/L9_2_BubbleSort_BigToSmall.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

20 lines
477 B
C++

#include <bits/stdc++.h>
using namespace std;
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8}; // 待排序数组
int main() {
int len = sizeof(a) / sizeof(int); // 数组长度
// 冒泡排序
for (int i = 0; i < len; i++)
for (int j = 0; j < len - 1 - i; j++)
if (a[j] < a[j + 1]) // 从大排到小
swap(a[j], a[j + 1]);
// 打印数组
for (int i = 0; i < len; i++) cout << a[i] << " ";
return 0;
}