#include<stdio.h>
#define MAX 50
enum bool{FALSE,TRUE};
int main()
{
int a[MAX],n,i;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter element %d : ",i+1);
scanf("%d",&a[i]);
}
printf("Unsorted list is :\n");
show(a,0,n-1);
printf("\n");
quick(a,0,n-1);
printf("Sorted list is :\n");
show(a,0,n-1);
return 0;
}
quick(int a[],int low,int up)
{
int loc,temp,left,right;
enum bool locpos=FALSE;
left=low;
right=up;
loc=low;
if(low>=up)
return;
printf("Sublist : ");
show(a,low,up);
while(locpos==FALSE)
{
while( a[loc]<=a[right] && loc!=right )
right=right-1;
if( loc==right )
locpos=TRUE;
if( a[loc] > a[right] )
{
a[loc]=a[loc]+a[right];
a[right]=a[loc]-a[right];
a[loc]=a[loc]-a[right];
loc=right;
}
while( a[loc]>=a[left] && left != loc )
left=left+1;
if(loc==left)
locpos=TRUE;
if( a[loc] < a[left] )
{
a[loc]=a[loc]+a[left];
a[left]=a[loc]-a[left];
a[loc]=a[loc]-a[left];
loc=left;
}
}
printf("-> item Placed is %d ->", a[loc]);
show(a,low,up);
printf("\n");
quick(a,low,loc-1);
quick(a,loc+1,up);
}
show(int a[],int low,int up)
{
int i;
for(i=low;i<=up;i++)
printf("%d\t",a[i]);
}
1 comment:
Post a Comment