Write c program to sort the elements from an array using quick sort

Quick Sort

#include<stdio.h>
#include<conio.h>

//To define function
void quick(int [], int , int );

void main()
{
   int a[20],n,i;
   printf("\nEnter the number of terms in array: ");
   scanf("%d",&n);
   for(i=0;i<n;i++)
   {
    printf("\nEnter element %d: ",i+1);
        scanf("%d",&a[i]);
   }
   quick(a,0,n-1);
   printf("\nSorted Array is: \n");
   for(i=0;i<n;i++)
   printf("%d ",a[i]);
   getch();
}

//To sort the elements
void quick(int a[],int l, int h)
{
   int high,low,key,temp;
   high=h;
   low=l;
   key=a[(high+low)/2];
   do
   {
      while(a[low]<key)
      low++;
      while(a[high]>key)
      high--;
      if(low<=high)
      {
       temp=a[high];
         a[high]=a[low];
         a[low]=temp;
         low++;
         high--;
      }
   }while(low<=high);
   if(l<high)
   quick(a,l,high);
   if(h>low)
   quick(a,low,h);
}

0 Comments