Friday 1 April 2016

QUICKSORT IMPLEMENTATION IN C++ (COREMAN)

#include<iostream.h>
#include<conio.h>
class xyz
{
int a[10],n;
public:
xyz();
void QUICKSORT(int [],int ,int );
int PARTITION1(int [],int,int );
void disp();
};
xyz::xyz()
{
    cout<<"Enter the size of the array"<<endl;
    cin>>n;
    cout<<"Enter the elements in the array"<<endl;
    for(int i=1;i<=n;i++)
    {
cin>>a[i];
    }
   // cout<<"sorting using quick sort"<<endl;
    int p=1,r=n;
    QUICKSORT(a,p,r);
}
void xyz::disp()
{
   cout<<"sorted form"<<endl;
   for(int i=1;i<=n;i++)
   {
       cout<<"\t"<<a[i];
   }
}
void xyz::QUICKSORT(int a[],int p,int r)
{
int q;
if(p<r)
{
q=PARTITION1(a,p,r);
QUICKSORT(a,p,q-1);
QUICKSORT(a,q+1,r);
}
}

int xyz::PARTITION1(int a[],int p,int r)
{
int temp,temp1;
int x=a[r];
int i=p-1;
for(int j=p;j<=r-1;j++)
{
   if(a[j]<=x)
   {

i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
   }
}
temp1=a[i+1];
a[i+1]=a[r];
a[r]=temp1;
return (i+1);
}
void main()
{       clrscr();
xyz x;
x.disp();
getch();
}

No comments:

Post a Comment