-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort.cpp
More file actions
53 lines (45 loc) · 909 Bytes
/
Copy pathquickSort.cpp
File metadata and controls
53 lines (45 loc) · 909 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
#include<iostream>
#include<vector>
using namespace std;
int partion(int i, int j, vector<int>&v){
int p = v[j];
int t = i-1;
for (int s = i; s<j;s++){
if (v[s]<p){
t++;
swap(v[s],v[t]);
}
}
swap(v[t+1],v[j]);
return t+1;
}
void quickSort(int i,int j, vector<int>&v){
if (i<j) {
int m = partion(i,j,v);
quickSort(i,m-1,v);
quickSort(m+1,j,v);
}
}
int main() {
vector<int> v;
cout << "\nEnter the lenght of elements\n";
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
int ele;
cin >> ele;
v.push_back(ele);
}
cout << "\n Elements before sorting\n";
for (auto i : v)
{
cout << i << " ";
}
quickSort(0, n - 1, v);
cout << "\n Elements after sorting\n";
for (auto i : v)
{
cout << i << " ";
}
}