-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
56 lines (33 loc) · 742 Bytes
/
QuickSort.cpp
File metadata and controls
56 lines (33 loc) · 742 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
49
50
51
52
53
54
55
56
#include<iostream>
#include<vector>
using namespace std;
int partition(vector<int>&arr,int st,int end){
int idx=st-1;
int pivot=arr[end];
for(int j=st;j<end;j++){
if(arr[j]<=pivot){
idx++;
swap(arr[j],arr[idx]);
}
}
idx++;
swap(arr[idx],arr[end]);
return idx;
}
void QuickSort(vector<int>&arr,int st,int end){
if(st<end){
int pividx= partition(arr,st,end);
//the call for left half
QuickSort(arr,st,pividx-1);
//the call for right half
QuickSort(arr,pividx+1,end);
}
}
int main(){
vector<int> arr ={12,31,35,8,32,17};
QuickSort(arr,0,arr.size()-1);
for(int val: arr){
cout<<val<<" ";
}
return 0;
}