C - Program to Implement Quick Sort
#include<iostream.h>
#include<conio.h>
#include<time.h>
class sort
{
public:
int a[100],n;
int lo,hi;
void get();
void disp();
void quicksort(int,int);
};
int main()
{
int i;
char k='y';
sort a;
do
{
clrscr();
float toc;
clock_t end,start;
start=clock();
cout<<"\n\t\t\t\tQuick Sort\n";
cout<<"\t\t\t\t----------\n\n";
a.get();
cout<<"\n\t\t\t*** Array Before Sorting ***\n\n";
a.disp();
a.quicksort(a.lo,a.hi);
cout<<"\n\n\n\t\t\t*** Array After Sorting ***\n\n";
a.disp();
end=clock();
toc=(end - start)/CLK_TCK;
cout<<"\n\tTime Complexity :: "<<toc;
cout<<"\n\n\t\t\tDo you want to Continue?(y/n)";
k=getche();
}while(k=='y');
return 0;
}
void sort::get()
{
int i;
cout<<"\n\tEnter Size of Array :: ";
cin>>n;
lo=0;
hi=n-1;
cout<<"\n\tEnter the Elements of Array :: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void sort::disp()
{
int i;
cout<<"\n\tElements of Array :: ";
for(i=0;i<n;i++)
{
cout<<" "<<a[i];
}
}
void sort::quicksort(int lo,int hi)
{
int i,j,k,x;
i=lo;
j=hi;
x=a[(lo+hi)/2];
do
{
while(a[i]<x)
i++;
while(a[j]>x)
j--;
if(i<=j)
{
k=a[i];
a[i]=a[j];
a[j]=k;
i++;
j--;
}
}while(i<=j);
if(lo<i-1)
quicksort(lo,i-1);
if(i<hi)
quicksort(i,hi);
}
#include<conio.h>
#include<time.h>
class sort
{
public:
int a[100],n;
int lo,hi;
void get();
void disp();
void quicksort(int,int);
};
int main()
{
int i;
char k='y';
sort a;
do
{
clrscr();
float toc;
clock_t end,start;
start=clock();
cout<<"\n\t\t\t\tQuick Sort\n";
cout<<"\t\t\t\t----------\n\n";
a.get();
cout<<"\n\t\t\t*** Array Before Sorting ***\n\n";
a.disp();
a.quicksort(a.lo,a.hi);
cout<<"\n\n\n\t\t\t*** Array After Sorting ***\n\n";
a.disp();
end=clock();
toc=(end - start)/CLK_TCK;
cout<<"\n\tTime Complexity :: "<<toc;
cout<<"\n\n\t\t\tDo you want to Continue?(y/n)";
k=getche();
}while(k=='y');
return 0;
}
void sort::get()
{
int i;
cout<<"\n\tEnter Size of Array :: ";
cin>>n;
lo=0;
hi=n-1;
cout<<"\n\tEnter the Elements of Array :: ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void sort::disp()
{
int i;
cout<<"\n\tElements of Array :: ";
for(i=0;i<n;i++)
{
cout<<" "<<a[i];
}
}
void sort::quicksort(int lo,int hi)
{
int i,j,k,x;
i=lo;
j=hi;
x=a[(lo+hi)/2];
do
{
while(a[i]<x)
i++;
while(a[j]>x)
j--;
if(i<=j)
{
k=a[i];
a[i]=a[j];
a[j]=k;
i++;
j--;
}
}while(i<=j);
if(lo<i-1)
quicksort(lo,i-1);
if(i<hi)
quicksort(i,hi);
}