Wednesday, 4 September 2013

QUICK SORT C PROGRAM


/*CSEMATTER.BLOGSPOT.IN
PROGRAM NAME:PROGRAM IN C FOR QUICK SORT*/
#include<stdio.h>
#include<conio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
void main()
{
int a[30],n,i;
clrscr();
printf("enter the no. of element:");

scanf("%d",&n);
printf("enter the element");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

quick_sort(a,0,n-1);
printf("\sorted elements\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
void quick_sort(int a[],int p,int r)
{int q;
if(p<r)
{
q=partition(a,p,r);
quick_sort(a,p,q-1);
quick_sort(a,q+1,r);}
}

int partition(int a[],int p,int r)
{
int x;
int i,temp,j;
i=p-1;
x=a[r];
for(j=p;j<r-1;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]=a[i+1];}
return(i+1);
}

No comments:

Post a Comment