Like us on Facebook and stand a chance to win pen drives!

Quick Sort Algorithm



#include<stdio.h>
int partition(int a[],int b,int c);
void qsort(int a[],int p,int r);
int main()
{
  int a[6],i;
  for(i=0;i<6;i++)
  {
    printf("a[%d]element  :",i);
    scanf("%d",&a[i]);
  }
    qsort(a,0,5);
int l;
for(l=0;l<6;l++)
{
    printf("Elements a[%d]=%d\n",l,a[l]);
}  
return 0;
}
int partition(int a[],int b,int c)
{
   int x,i,j,temp1,temp2;
     x= a[c];
     i=b-1;
 for(j=b;j<5;j++)
 {
    if(a[j]<=x)
     {
         i=i+1;
         temp1=a[i];
         a[i]=a[j];
         a[j]=temp1;
      }
  }
    temp2=a[5];
    a[5]=a[i+1];
    a[i+1]=temp2;

 printf("Partition point is  %d\n",i+1);
 return(i+1);

}   
void qsort(int a[],int p,int r)
{
  int q;
   if(p>=r)
    {
       return;
    }
  else
    {
      q=partition(a,p,r);
      qsort(a,p,q-1);
      qsort(a,q+1,r);
    }
    }






Comment with Facebook


Copyright © 2012 SLIIT Helper.