C++实现冒泡排序
自学视频:https://www.bilibili.com/video/av26394341?p=6(刚开始学,还是小白,希望大家互相交流共同进步!)
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Solution{
public:
void sortArray(vector<int>* nums)
{
for(int i = 0; i < nums -> size(); i++)
{
for(int j = 0; j < nums-> size() - i; j++)
{
if((*nums) < (*nums))
{
int temp = (*nums);
(*nums) = (*nums);
(*nums) = temp;
}
}
}
}
};
int main()
{
int num;
cout << "send number" << endl;
vector<int> list;
while(cin >> num)
{
list.push_back(num);
}
Solution test;
test.sortArray(&list);
vector<int>::iterator it;
for(it = list.begin(); it != list.end(); it++)
{
cout << *it <<" ";
}
return 0;
}
页:
[1]