You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
python/TangDou/LuoGuBook/L9_2_BubbleSort_SmallToBig.cpp

20 lines
467 B

#include <bits/stdc++.h>
using namespace std;
int a[] = {1, 3, 5, 7, 9, 2, 4, 6, 8}; // 待排序数组
int main() {
int len = sizeof(a) / 4; // 数组长度
// 冒泡排序
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;
}