简单来说 两层循环 第一层是排序几轮 第二层是每轮排序多少次
第一层:len-1
第二层: len-i-1
正序排列:从小到大
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void bubblesort(vector<int>&array) {
for (int i = 0; i < array.size() - 1; i++) {
for (int j = 0; j < array.size() - i - 1; j++) {
if (array[j] > array[j + 1]) {
swap(array[j], array[j + 1]);
}
}
}
}
int main() {
vector<int> array;//定义一个vector数组array
int number;
cin >> number;
int s;
for (int i = 0; i < number; i++) {
cin >> s;
array.push_back(s);
}
//个人建议 直接 vector<int>array{要处理的数组}, 上面就都省略了 用网上的while或者getchar都是把你输入的当成vector中的一个元素
bubblesort(array);
for (auto x : array) {
cout << x << ' ';
}
system("pause");
return 0;
}