#include<stdio.h>
void qsort(int b[10],int left,int right)
{
int i,j,p,temp,k,flag;
if(right>left)
{
i=left;
j=right;
p=b[left];
flag=0;
while(!flag)
{
do{
i++;
}while((b[i]<=p) && (i<=right));
while((b[j]>=p) && (j>left))
{
j--;
}
if(j<i)
flag=1;
else
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
temp=b[left];
b[left]=b[j];
b[j]=temp;
qsort(b,left,j-1);
qsort(b,i,right);
}
}
main()
{
int a[10],i,n,r,l;
printf("\n Enter the number of elements=");
scanf("%d",&n);
printf("\n Enter the elements=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=0;
r=n-1;
qsort(a,l,r);
printf("\n After quick sort=");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
void qsort(int b[10],int left,int right)
{
int i,j,p,temp,k,flag;
if(right>left)
{
i=left;
j=right;
p=b[left];
flag=0;
while(!flag)
{
do{
i++;
}while((b[i]<=p) && (i<=right));
while((b[j]>=p) && (j>left))
{
j--;
}
if(j<i)
flag=1;
else
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
temp=b[left];
b[left]=b[j];
b[j]=temp;
qsort(b,left,j-1);
qsort(b,i,right);
}
}
main()
{
int a[10],i,n,r,l;
printf("\n Enter the number of elements=");
scanf("%d",&n);
printf("\n Enter the elements=");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=0;
r=n-1;
qsort(a,l,r);
printf("\n After quick sort=");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
No comments:
Post a Comment