#include
#include
class QuickSort
{
int a[10],n;
public:
void read();
void disp();
void qsort();
void quicksort(int,int);
int split(int,int);
};
void QuickSort::read()
{
cout<<"Enter the no of elements:";
cin>>n;
cout<<"Enter the elements:";
for(int i=0;i
}
void QuickSort::disp()
{
cout<<"\n\nSorted Array:\n\n";
for(int i=0;i
void QuickSort::qsort()
{
quicksort(0,n-1);
}
void QuickSort::quicksort(int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(lower,upper);
quicksort(lower,i-1);
quicksort(i+1,upper);
}
}
int QuickSort::split(int lower,int upper)
{
int i,p,q,temp;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p] p++;
while(a[q]>i)
q--;
if(q>p)
{
temp=a[q];
a[q]=a[p];
a[p]=temp;
}
}
temp=a[lower];
a[lower]=a[q];
a[q]=temp;
return q;
}
void main()
{
QuickSort qs;
clrscr();
qs.read();
qs.qsort();
qs.disp();
getch();
}
No comments:
Post a Comment