#include<stdio.h>
quicksort(int *,int,int);
partition(int *,int,int);
int main()
{
int a[100],p,r,i=0,n;
clrscr();
printf("Enter the length of array: ");
scanf("%d",&n);
printf("Enter the elements of array: \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("The sorted sequence is: \n");
for(i=0;i<n;i++)
printf(" %d ",a[i]);
getch();
return 0;
}
quicksort(int *a,int p,int r)
{
int q;
if(p<r)
{
q=partition(a,p,r);
quicksort(a,p,q-1);
quicksort(a,q+1,r);
}
return 0;
}
int partition(int *a,int p,int r)
{
int x,i,j,temp;
x=a[r];
i=p-1;
for(j=p;j<r;j++)
{
if(a[j]<=x)
{
i=i+1;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[i+1];
a[i+1]=a[r];
a[r]=temp;
return i+1;
}
No comments:
Post a Comment