Tuesday, 24 January 2012

Quick sort


/*Author-Neelkant.S.Patil,GMIT,Davanagere*/


#include<stdio.h>
#include<conio.h>
#include<time.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
int partition(int a[],int l,int r)
{
int i,j;
int p;
p=a[l];
i=l+1;
j=r;
while(i<=j)
{
delay(100);
while(a[i]<=p)
i++;
while(a[j]>p)
j--;
swap(&a[i],&a[j]);
}
swap(&a[i],&a[j]);
swap(&a[l],&a[j]);


return j;
}
void qsort(int a[],int l,int r)
{
int s;
if(l<r)
{
s=partion(a,l,r);
qsort(a,l,s-1);
qsort(a,s+1,r);
}
}
void main()
{
int a[20],i,j,n;
clock_t begin,end;
clrscr();
printf("enter the no of elements\n");
scanf("%d",&n);
printf("enter the elements:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);


begin=clock();
qsort(a,0,n-1);
end=clock();
printf("after sorting the array is:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("Begin time=%d\n",begin);
printf("End time=%d\n",end);
printf("Total number of ticks=%d\n",(end-begin));
printf("Total time required=%f\n",((end-begin)/CLK_TCK));
getch();
}



No comments:

Post a Comment