forked from puruagarwal1/hacktoberfest-2022-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
48 lines (37 loc) · 803 Bytes
/
QuickSort.cpp
File metadata and controls
48 lines (37 loc) · 803 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 <iostream>
#include <vector>
using namespace std;
int partition(vector<int> &a, int s, int e){
int pivot = a[e];
int i = s - 1;
for (int j = s; j < e; j++){
if (a[j] < pivot){
i++;
swap(a[i], a[j]);
}
}
swap(a[i + 1], a[e]);
return i + 1;
}
void quickSort(vector<int> &a, int s, int e){
// base case
if (s >= e){
return;
}
// rec case
int pre = partition(a, s, e);
quickSort(a, s, pre - 1);
quickSort(a, pre + 1, e);
}
int main(){
vector<int> a{12, 1, 34, 2, 53, 5};
int s = 0;
int e = a.size() - 1;
// Calling quickSort for sorting
quickSort(a, s, e);
// printing sorted array
for (int x : a){
cout << x << ", ";
}
return 0;
}