Sort elements using quick sort - C Program

C program to sort 'n' numbers using quick sort.

Solution:

Quick sort is also based on the 'Divide & Conquer' algorithm. In this sorting technique, an element is picked as a pivot and the array is partitioned around the pivot element. The target of each partition is, to put all smaller elements before pivot & put all greater elements after the pivot. This process is completed in linear time.

#include<stdio.h>
void quicksort(int x[20],int first,int last)
{
      int pivot,j,temp,i;
      if(first<last)
      {
            pivot=first;
            i=first;
            j=last;

            while(i<j)
            {
                  while(x[i]<=x[pivot]&&i<last)
                        i++;
                  while(x[j]>x[pivot])
                        j--;
                  if(i<j)
                  {
                        temp=x[i];
                        x[i]=x[j];
                        x[j]=temp;
                  }
            }
            temp=x[pivot];
            x[pivot]=x[j];
            x[j]=temp;
            quicksort(x,first,j-1);
            quicksort(x,j+1,last);
      }
}
int main()
{
      int x[20],size,i;
      printf("\tQuick sort\n");
      printf("-----------------------------------\n");
      printf(" How many numbers you want to sort?: ");
      scanf("%d",&size);
      printf("\n Enter %d elements: \n",size);
      for(i=0;i<size;i++)
            scanf("%d",&x[i]);
      quicksort(x,0,size-1);
      printf("\n Sorted elements after applying quick sort: \n\n");
      for(i=0;i<size;i++)
            printf(" %d",x[i]);
      return 0;
}


Output:

quick sort