-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort
49 lines (37 loc) · 827 Bytes
/
QuickSort
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
49
#include<bits/stdc++.h>
using namespace std;
int partition(int *v,int l,int r){
int pivot=v[r];
int index=l;
for(int i=l;i<=r-1;i++){
if(v[i]<=pivot){
int temp=v[i];
v[i]=v[index];
v[index]=temp;
index++;
}
}
int temp=v[r];
v[r]=v[index];
v[index]=temp;
return index;
}
void quicksort(int *v,int l,int r){
if(l<r){
int j=partition(v,l,r);
quicksort(v,l,j-1);
quicksort(v,j+1,r);
}
}
int main(){
int n;
cin>>n;
int v[n];
for(int i=0;i<n;i++){
cin>>v[i];
}
quicksort(v,0,n-1);
for(int i=0;i<n;i++){
cout<<v[i]<<" ";
}
}