forked from vrankusharma/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSA2.cpp
More file actions
57 lines (47 loc) · 1.06 KB
/
Copy pathDSA2.cpp
File metadata and controls
57 lines (47 loc) · 1.06 KB
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
#include <iostream>
using namespace std;
int partition(int *a,int s,int e){
int i = s-1;
int j = s;
int pivot = a[e];
for( ;j<e;j++){
if(a[j]<=pivot){
i++;
swap(a[j],a[i]);
}
}
//Bring the Pivot Element to proper index
swap(a[i+1],a[e]);
return i+1;
}
void quickSort(int *a,int s,int e){
//Base Condition
if(s>=e)
return;
//Get the position of Pivot Element
int p = partition(a,s,e);
//Call the function recursively to apply the same for two parts of Array
quickSort(a,s,p-1);
quickSort(a,p+1,e);
}
int main()
{
int a[100];
cout<<"Please Enter the Size of Array"<<endl;
int n;
cin>>n;
cout<<"Please Enter the Elements in the Array"<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
cout<<"Elements in the Array are:- "<<endl;
for(int i=0;i<n;i++){
}
//function call
quickSort(a,0,n-1);
//Sorted Elements in the Array
for(int i=0;i<n;i++){
cout<<a[i]<<" ";
}
return 0;
}