-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort_vector.cpp
More file actions
59 lines (42 loc) · 967 Bytes
/
Copy pathquickSort_vector.cpp
File metadata and controls
59 lines (42 loc) · 967 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
57
58
59
#include <bits/stdc++.h>
using namespace std;
//VECTOR METHOD
void quicksort(vector<int> &vec, int L, int R) {
int i, j, mid, piv;
i = L;
j = R;
mid = L + (R - L) / 2;
piv = vec[mid]; //pivot is in the middle
while (i<R || j>L) {
while (vec[i] < piv)
i++;
while (vec[j] > piv)
j--;
if (i <= j) {
swap(vec[i], vec[j]); //STL swap handle
i++;
j--;
}
else {
if (i < R)
quicksort(vec, i, R);
if (j > L)
quicksort(vec, L, j);
return;
}
}
}
int main() {
int n;
vector<int> nums;
cin>>n; //no of elements
for(int i=0;i<n;i++){
int value;
cin >> value;
nums.push_back(value);
}
quicksort(nums, 0, n - 1);
for(auto &v: nums){
cout << v << " ";
}
}