Quick Sort

#include<stdio.h>
#include<conio.h>
void quickSort(int a[],int ,int);
int main ()
{
	int n,i,a[10];
   clrscr();
   printf("How many no you want to enter: ");
   scanf("%d",&n);
   printf("Enter the no:\n");
   for(i=0;i<n;i++)
   	scanf("%d",&a[i]);
   quickSort(a,0,n-1);
   printf("After Quick Sorting:\n");
   for(i=0;i<n;i++)
   	printf("%d ",a[i]);
   getch();
   return 0;
}

void quickSort(int a[],int low,int high)
{
	int l,r,loc;
   l=low, r=high;
   if(l<r)
   {
   	loc=partition(a,l,r);
      quickSort(a,l,loc-1);
      quickSort(a,loc+1,r);
   }
}

int partition(int a[],int left,int right)
{
	int loc=left;
   while(left<right)
   {
   	while((a[loc]<=a[right])&& loc<right)
      	right--;
      if(a[loc]>a[right])
      {
      	a[loc]=(a[loc]+a[right]-(a[right]=a[loc]));
         loc=right;
         left++;
      }
      while((a[loc]>=a[left])&& loc>left)
      	left++;
      if(a[loc]<a[left])
      {
      	a[loc]=(a[loc]+a[left]-(a[left]=a[loc]));
         loc=left;
         right--;
      }
   }
   return loc;
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *