C++实现快速排序
自学视频:https://www.bilibili.com/video/av62621532?from=search&seid=13015703200680132216参考代码:https://blog.csdn.net/helloworld_ptt/article/details/86170077
(刚开始学,还是小白,希望大家互相交流共同进步!)
#include<iostream>
#include<vector>
using namespace std;
class Solution{
public:
void sorted(vector<int>* nums, int left, int right)
{
int i = left;
int j = right;
int pivot = (*nums);
if(i < j)
{
while(i < j)//i == j stop
{
while(i < j && (*nums) >= pivot) j--;
if(i < j)
{
(*nums) = (*nums);
i++;
}
while(i < j && (*nums) <= pivot) i++;
if(i < j)
{
(*nums) = (*nums);
j--;
}
}
(*nums) = pivot;
sorted(nums, left, i - 1);
sorted(nums, i + 1, right);
}
}
};
int main(void)
{
int num;
cout << "send number" << endl;
vector<int> list;
while(cin >> num)
{
list.push_back(num);
}
Solution test;
test.sorted(&list, 0, list.size()-1);
vector<int>::iterator it;
for(it = list.begin(); it != list.end(); it++)
{
cout << *it <<" ";
}
return 0;
}
页:
[1]