32)Write a C program to sort array element using quick sort method.


#include<stdio.h>
#include<conio.h>
int n,i=1;
void display(int a[])
{
int i;
printf(“\n”);
for(i=0;i<n;i++)
printf(“\t%d”,a[i]);
}
int partition(int a[],int lb,int ub)
{
int up,down;
int temp,pivot;
up=ub;
down=lb+1;
pivot=a[lb];
do
{
while((a[down]<pivot) && (down<=ub))
down++;
while((a[up]>pivot) && (up>lb))
up–;
if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
display(a);
}while(down<up);
//interchange pivot and a[up]
a[lb]=a[up];
a[up]=pivot;
display(a);
return up;
}
void quicksort(int a[],int lb,int ub)
{
int j;
if(lb<ub)
{
printf(“\nIteration %d\n”,i);
display(a);
i++;
j=partition(a,lb,ub);
quicksort(a,lb,j-1);
quicksort(a,j+1,ub);
}
}
void main()
{
int a[20],i;
clrscr();
printf(“\nEnter number of elements: “);
scanf(“%d”,&n);
printf(“\nEnter the unsorted numbers: “);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
quicksort(a,0,n-1);
printf(“\nThe sorted list: “);
display(a);
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s