forked from Ananya02850/HacktoberFest-2022
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
55 lines (54 loc) · 1.22 KB
/
Copy pathQuickSort.cpp
File metadata and controls
55 lines (54 loc) · 1.22 KB
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
50
51
52
53
54
55
#include<bits/stdc++.h>
using namespace std;
// int rpartition(int a[],int lo,int hi){
// store the randon pivot in a variable
// then swap random with first or last as per wish
// then simply call partion accordingly
// i.e by swapping we are converting them into as first or last pivot
// }
// taking first element as pivot
int partition(int a[],int lo,int hi){
int pivot = a[lo];
int i = hi+1;
for(int j=hi;j>=lo;j--){
if(a[j]>pivot){
i--;
swap(a[i],a[j]);
}
}
swap(a[i-1],a[lo]);
return i-1;
}
// // taling last element as pivot
// int partition(int a[],int lo,int hi){
// int pivot = a[hi];
// int i = lo-1;
// for(int j=lo;j<=hi;j++){
// if(a[j]<pivot)
// {
// i++;
// swap(a[i],a[j]);
// }
// }
// swap(a[i+1],a[hi]);
// return i+1;
// }
void quickSort(int a[],int lo,int hi){
if(lo>hi)
return;
int pi = partition(a,lo,hi);
quickSort(a,lo,pi-1);
quickSort(a,pi+1,hi);
}
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
quickSort(a,0,n-1);
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
}