forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.cpp
More file actions
49 lines (47 loc) · 857 Bytes
/
quicksort.cpp
File metadata and controls
49 lines (47 loc) · 857 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
#include<iostream>
using namespace std;
int comp=0;
int Partition(int a[],int p,int q)
{
int x=a[p];
int i=p,temp;
for(int j=p+1;j<=q;j++)
{
if(a[j]<=x)
{
i++;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i];
a[i]=x;
a[p]=temp;
for(int c=0;c<6;c++)
cout<<a[c]<<"\t";
cout<<endl;
return i;
}
void Quicksort(int a[],int p,int q)
{
if(p<q)
{
comp=comp+q-p;
cout<<comp;
int r=Partition(a,p,q);
Quicksort(a,p,r-1);
Quicksort(a,r+1,q);
}
}
int main()
{
int n,a[10000];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
Quicksort(a,0,n-1);
for(int i=0;i<n;i++)
cout<<a[i];
cout<<endl<<"final answer"<<comp;
}