Quick Sort > C Program

Analysis of Algorithm

Quick Sort > C Program


#include<stdio.h>
#include<conio.h>
void quick(int a[],int lb,int ub);
int partition(int a[],int lb,int ub);
void main()
{       

 int a[50],i,j,n;
clrscr();
printf("Enter the number of elements:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%d",&a[i]);
}
quick(a,0,n-1);
printf("The sorted array:");
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
//scanf("%d",&a[i]);
}
getch();
}
void quick(int x[],int lb,int ub)
{
int j ;
if(lb>ub)
return;
j=partition(x,lb,ub);
quick(x,lb,j-1);
quick(x,j+1,ub);
}
int partition(int x[],int lb,int ub)
{
int i,a,temp,down,up;
a=x[lb];
down=lb;
up=ub;
while(down<up)
{
while(x[down]<=a && down<ub)
down++;
while(x[up]>a)
up--;
if(down<up)
{
temp=x[down];
x[down]=x[up];
x[up]=temp;
}

}
x[lb]=x[up];
x[up]=a    ;
return up;

}

Comments

Popular posts from this blog

Intermediate Code Generation > C Program