#include <iostream>
#include <vector>
using namespace std;
void Swap(int &a,int &b)
{
int tmp = a;
a = b;
b = tmp;
}
void Show(vector<int> & vec)
{
vector<int> :: iterator it = vec.begin();
for(it;it != vec.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
}
int Quick_one(vector<int> & vec,int low,int high)
{
int tmp = vec[low];
while(low < high)
{
while(low < high && vec[high] >= tmp)
{
high--;
}
vec[low] = vec[high];
while(low < high && vec[low] <= tmp)
{
low++;
}
vec[high] = vec[low];
}
vec[low] = tmp;
return low;
}
void QuickSort(vector<int> & vec,int low,int high)//时间复杂度O(nlogn) 空间复杂度O(logn) 不稳定
{
int pivot;
if(low < high)
{
pivot = Quick_one(vec,low,high);
QuickSort(vec,low,pivot - 1);
QuickSort(vec,pivot +1,high);
}
}
int main()
{
int arr[10] = {10,9,8,7,6,4,5,2,1,3};
vector<int> vec(arr,arr+10);
Show(vec);
QuickSort(vec,0,vec.size()-1);
Show(vec);
}
快排
最新推荐文章于 2024-08-28 19:43:12 发布