Translate

Labels

Thursday, 28 February 2013

QUICK SORT



# include <stdio.h>
int a[100],l;
main()
{
int m,n,i;
clrscr();
printf("\t\t\t\tQUICK SORT\n");
printf("\nEnter the no. of elements:\n");
scanf("%d",&n);
l=n;
printf("\nEnter the elements:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
m=1;
    printf("\nIterations:\n");
quick(m,n);
for(i=1;i<=n;i++)
printf("%d  ",a[i]);
printf("\n\nThe sorted elements are...\n");
for(i=1;i<=n;i++)
printf("%d  ",a[i]);
getch();
}
quick(int m,int n)
{
int h,i,j,k,z;
for(z=1;z<l;z++)
{
if(a[z]<=a[z+1])
h=1;
else
{
h=0;
break;
}
}
if(h==0)
{
for(z=1;z<=l;z++)
printf("%d  ",a[z]);
printf("\n");
}
if(m<n)
{
i=m;
j=n+1;
k=a[m];
while(i<j)
{
do
i++;
while(a[i]<k);
do
j--;
while(a[j]>k);
if(i<j)
interchange(i,j);
}
interchange(m,j);
quick(m,j-1);
quick(j+1,n);
}
}
interchange(int x,int y)
{
int t,z;
t=a[x];
a[x]=a[y];
a[y]=t;
}



No comments: