-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathquick_sort.cpp
More file actions
48 lines (48 loc) · 844 Bytes
/
quick_sort.cpp
File metadata and controls
48 lines (48 loc) · 844 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long l;
typedef long double ld;
void quick_sort(vector<int> &v,int start,int end)
{
int pivot=end;
end--;
int i=start-1;
for(int j=start;j<=end;++j)
{
if(v[j]<=v[pivot])
{
++i;
swap(v[i],v[j]);
}
}
swap(v[pivot],v[i+1]);
if(end-start+1>=2)
{
if(start<=i)
{
quick_sort(v,start,i);
}
if(i+2<=pivot)
{
quick_sort(v,i+2,pivot);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<int> v(n);
for(int i=0;i<n;++i)
{
cin>>v[i];
}
quick_sort(v,0,v.size()-1);
for(int i=0;i<v.size();++i)
{
cout<<v[i]<<" ";
}
return 0;
}