#include<stdio.h>
int a[20];
int partition(int p, int r)
{
int x=a[r];
int i=p-1;
int j;
for(j=p; j<=r-1;j++)
{
if(a[j]<=x)
{
i++;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
void quicksort(int p,int r)
{
if(p<r)
{
int q=partition(p,r);
quicksort(p,q-1);
quicksort(q+1,r);
}
}
void main()
{
int n,i;
printf("\n Enter no of elements");
scanf("%d",&n);
printf("\nEnter elements:");
for( i=1;i<=n;i++)
scanf("%d",&a[i]);
quicksort(1,n);
printf("\n After sorting:");
for( i=1;i<=n;i++)
printf("%d ",a[i]);
}
int a[20];
int partition(int p, int r)
{
int x=a[r];
int i=p-1;
int j;
for(j=p; j<=r-1;j++)
{
if(a[j]<=x)
{
i++;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
void quicksort(int p,int r)
{
if(p<r)
{
int q=partition(p,r);
quicksort(p,q-1);
quicksort(q+1,r);
}
}
void main()
{
int n,i;
printf("\n Enter no of elements");
scanf("%d",&n);
printf("\nEnter elements:");
for( i=1;i<=n;i++)
scanf("%d",&a[i]);
quicksort(1,n);
printf("\n After sorting:");
for( i=1;i<=n;i++)
printf("%d ",a[i]);
}
No comments:
Post a Comment