-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathquick.cpp
64 lines (53 loc) · 809 Bytes
/
quick.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
#include<iostream>
using namespace std;
void swap(int *a,int *b){
int t=*a;
*a=*b;
*b=t;
}
void in(int a[],int n){
for(int i=0;i<n;i++)
cin>>a[i];
}
void show(int a[],int n){
for(int y=0;y<n;y++)
cout<<a[y]<<" ";
cout<<endl;
}
int hoare_leftpartition(int a[],int i,int n){
int pi=i;
int l=i;
int r=n;
while(l<r){
while(a[++l]<a[pi] and l<=r){
}
while(a[r]>a[pi] &&r>0){
r--;
}
if(l>=r)
break;
else
swap(&a[l],&a[r]);
}
//if(l=r)
// if(l)
swap(&a[r],&a[pi]);
return r;
}
void quick(int a[],int i,int r){
if(i>=r)
return;
int m=partition(a,i,r);
// cout<<"m-- "<<m<<" i- "<<i<<endl;
quick(a,i,m-1);
quick(a,m+1,r);
}
int main(void){
int n;
cout<<"Enter no of elements-";
cin>>n;
int a[n];
in(a,n);
quick(a,0,n-1);
show(a,n);
}