#include<iostream.h>
class quick
{
int i,j,n,a[10];
public:
void getdata()
{
cout<<"\n*****Quick Sort*****\n";
cout<<"\n--------------------\n";
cout<<"\nEnter the no.of elements:";
cin>>n;
cout<<"\nEnter the elements:";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
}
void sort()
{
qsort(1,n);
}
void qsort(int low,int hi)
{
int i,j,temp;
if (low<=hi)
{
i=low;
j=hi+1;
do
{
do
i++;
while ((a[i]<=a[low])&&(i!=hi));
do
j--;
while (a[j]>=a[low] &&(j!=low));
if (i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
break;
}
while (i<=j);
temp=a[low];
a[low]=a[j];
a[j]=temp;
display();
qsort(low,j-1);
qsort(j+1,hi);
}
}
void display()
{
cout<<"\nThe elements are:";
for (i=1;i<=n;i++)
cout<<a[i]<<" ";
}
};
main()
{
quick b1;
b1.getdata();
b1.sort();
b1.display();
}
0 comments:
Post a Comment