#include<iostream.h>
#include<conio.h>
class qsort
{
public:
int a[6],n;
void getdata();
void display();
void quicksort(int first, int last);
};
void qsort::getdata()
{
int i;
cout<<" \n Enter the no. of Elements";
cin>>n;
for(i=0;i<n;i++)
{
Cin>>a[i];
}
}
void qsort::display()
{
int i;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
}
void qsort::quicksort(int first, int last)
{
int t, pivot, i, j;
i = first;
j = last;
pivot = a[first];
if(i<j)
{
while(i < j)
{
while(a[i]<=pivot && i<last)
i = i + 1;
while(a[j]>=pivot && j >first)
j = j -1;
if(i<j)
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
t = a[first];
a[first] = a[j];
a[j] = t;
quicksort(first,j-1);
quicksort(j+1,last);
}
}
void main()
{
qsort q;
q.getdata();
cout<<" before sorting: "<<endl;
q.display();
q.quicksort(0,q.n-1);
cout<< "after sorting:"<<endl;
q.display();
}
Comments
Post a Comment