常见排序算法之冒泡排序的C++实现
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
void bubbleSort(vector<int>& v)
{
int size = v.size();
for (int i = 0; i < size; ++i)
{
for (int j = 0; j < size - i - 1; ++j)
{
if (v[j] > v[j + 1])
swap(v[j], v[j + 1]);
}
}
}
};
int main()
{
Solution so;
vector<int> v = { 10, 2, 9, 1, 7, 0, 4, 3, 5, 8, 6 };
for (int x : v)
cout << x << " ";
cout << endl;
so.bubbleSort(v);
for (int x : v)
cout << x << " ";
return 0;
}