EX.NO:17
QUICK SORT
PROGRAM: #include<stdio.h> #include void swap(int a[],int j,int i); void quick(int a[],int first,int last) void main() { int a[20],n,i,f,l; clrscr(); printf("Enter the number of elements\n"); scanf("%d",&n); printf("\nEnter the elements\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); f=1; l=n; quick(a,f,l); printf("The sorted elements\n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); getch(); } void swap(int a[],int i,int j) { int temp; temp=a[i]; a[i]=a[j]; a[j]=temp; } void quick(int a[],int first,int last) { int i,j,pivot; if(first=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); } } OUTPUT: Enter the number of elements 5 Enter the elements 32 3 5 56 21 The sorted elements 3 5 21 32 56