## C program to sort the given list of elements using quick sort

__CONSTRUCTION:__

- Read the array elements
- Set the first element in that array as pivot element.
- Reorder the list so that all elements which are less than the pivot come before the pivot and so that all elements greater than the pivot come after it.
- After this partitioning, the pivot is in its final position. This is called the
**partition** - Repeat the steps from 2 to sort the sub-list of lesser elements and the sub-list of greater elements.

__PROGRAM:__

#include<stdio.h> #include<conio.h> #include<math.h> int i,j,n,pivot,a[20]; void quick(int a[],int left, int right); void swap(int a[],int i, int j); void main() { int a[20],n,i; clrscr(); printf("\n\tQUICK SORT"); printf("\nEnter the number of elements:"); scanf(" %d",&n); printf("Enter the elements:"); for(i=1;i<=n;i++) scanf(" %d",&a[i]); quick(a,1,n); printf("\n The sorted elements in the array are:"); for(i=1;i<=n;i++) printf(" %d",a[i]); getch(); } void quick(int a[],int first,int last) { if(first<last) { pivot=a[first]; i=first; j=last; while(i<j) { while(a[i]<=pivot&&i<last) i++; while(a[j]>=pivot&&j>first) j--; if(i<j) swap(a,i,j); } swap(a,first,j); quick(a,first,j-1); quick(a,j+1,last); } } void swap(int a[],int i,int j) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; }