-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_sort.cpp
More file actions
43 lines (39 loc) · 773 Bytes
/
Quick_sort.cpp
File metadata and controls
43 lines (39 loc) · 773 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
#include <bits/stdc++.h>
using namespace std;
vector<int> a;
int partition(int l, int h){
int pi = a[l];
int i = l;
int j = h;
while(i < j){
while(a[i] <= pi) i++;
while(a[j] > pi) j--;
if(i < j){
swap(a[i], a[j]);
}
}
swap(a[l], a[j]);
return j;
}
void quickSort(int l, int h){
if(l < h){
int pivot = partition(l, h);
quickSort(l, pivot - 1);
quickSort(pivot + 1, h);
}
}
int main(){
int n;
cout<<"Enter size of array:"<<endl;
cin>>n;
a.resize(n);
cout<<"Enter elements of array:"<<endl;
for(int i = 0; i < n; i++){
cin>>a[i];
}
quickSort(0, n-1);
for(int i = 0; i < n; i++){
cout<<a[i]<<" ";
}
return 0;
}