You are on page 1of 3

AIM :- To implement Quick Sort.

CODING :-

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i;
void quicksort(int[],int,int);
clrscr();
printf("Enter the number of element : ");
scanf("%d",&n);
printf("\nEnter the %d Numbers : \n\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
printf("\nNumbers Sorted By Quick Sort Are : \n\n");
for(i=0;i<n;i++)
printf("%d, ",a[i]);
getch();
}
void quicksort(int a[],int start,int end)
{
int x=0,temp,loc = start;
if(start<end)
{
while(x==0)
{
int left = start, right = end;
while((a[loc]<=a[right]) && (loc != right))
right=right-1;
if(loc==right)
{
quicksort(a,start,loc-1);
quicksort(a,loc+1,end);
return;
}
if(a[loc]>a[right])
{
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
}
while((a[left]<=a[loc]) && (loc!=left))
left=left+1;
if(loc == left)
{
quicksort(a,start,loc-1);
quicksort(a,loc+1,end);
return;
}
if(a[loc]<a[left])
{
temp = a[loc];
a[loc] = a[left];
a[left] = temp;
loc = left;
}
}
}
}
OUTPUT :-

Enter the number of element : 10

Enter the 10 Numbers :

10
25
46
34
5
87
91
67
28
39

Numbers Sorted By Quick Sort Are :

5, 10, 25, 28, 34, 39, 46, 67, 87, 91,

You might also like