Preview:
#include <stdio.h>
#include<time.h>
#define max 500
time_t start,end;
double tc;
void qsort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("enter the total number of elements :\n ");
scanf("%d",&n);
printf("\nEnter the elements to be sorted :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nArray elements before sorting :\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
start=clock();
qsort(a,0,n-1);
end=clock();
printf("\nArray elements after sorting :\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
tc=difftime(end,start)/CLOCKS_PER_SEC;
printf("time efficiency is %lf",tc);
}
 
void qsort(int a[],int low,int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
qsort(a,low,j-1);
qsort(a,j+1,high);
}
}
 
int partition(int a[],int low,int high)
{
int pivot,i,j,temp;
pivot=a[low];
i=low;
j=high+1;
do
{
do
i++;
while(a[i]<pivot&&i<=high);
do
j--;
while(pivot<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[low]=a[j];
a[j]=pivot;
return(j);
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter