forked from shubham7668/hacktober
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quickSort.cpp
89 lines (72 loc) · 1.61 KB
/
quickSort.cpp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*-----------------CODED BY ROCKHOPPER130-----------------*/
#include <bits/stdc++.h>
#include <numeric>
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define int long long int
#define FOR(i, j, k, in) for (int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for (int i=j ; i>=k ; i-=in)
#define REP(i, j, k) FOR(i, j, k, 1)
#define RREP(i, j, k) RFOR(i, k, j, 1)
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;
typedef std::vector<std::string> VS;
typedef std::vector<PII> VII;
typedef std::vector<VI> VVI;
typedef std::map<int,int> MPII;
typedef std::set<int> SETI;
typedef std::multiset<int> MSETI;
using namespace std;
///*------------------------FUNCTIONS-----------------------*///
void quickSort(vector <int> &v,int l,int r){
int i = l, j = r;
int pivot = v[(l + r) / 2];
while (i <= j){
while (v[i] < pivot){
i++;
}
while (v[j] > pivot){
j--;
}
if (i <= j){
swap(v[i],v[j]);
i++;
j--;
}
};
if (l < j){
quickSort(v, l, j);
}
if (i < r){
quickSort(v, i, r);
}
}
///*--------------------CODE BEGINS HERE--------------------*///
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
VI v;
v.push_back(2);
v.push_back(3);
v.push_back(6);
v.push_back(4);
v.push_back(9);
v.push_back(5);
v.push_back(1);
v.push_back(2);
v.push_back(7);
v.push_back(4);
int l=v.size();
REP(i,0,l){
cout << v[i] << " ";
}
cout<<"\n";
quickSort(v, 0, l-1);
REP(i,0,l){
cout << v[i] << " ";
}
cout<<"\n";
return 0;
}