/*
quick sort*/
#include<stdio.h>
int
a[20],n;
void
sort(int a[],int lb,int ub)
{
int p;
if(lb>=ub)
return;
p=partetion(a,lb,ub);
sort(a,lb,p-1);
sort(a,p+1,ub);
}
int
partetion(int a[],int lb,int ub)
{
int down,up,key,t;
down=lb;
up=ub;
key=a[lb];
while(down<up)
{
while((key>=a[down])
&&(down<up))
down++;
while(key<a[up])
up--;
if(down<up)
{
t=a[down];
a[down]=a[up];
a[up]=t;
}
}
a[lb]=a[up];
a[up]=key;
return up;
}
main()
{
int i;
printf("enter the number of
elements:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter element
%d:",i);
scanf("%d",&a[i]);
}
printf("unsorted list:");
for(i=1;i<=n;i++)
printf("\t%d",a[i]);
sort(a,1,n);
printf("\nsorted list:\n");
for(i=1;i<=n;i++)
printf("%d\t",a[i]);
}
.......OUTPUT.......
enter
the number of elements:5
enter
element 1:6
enter
element 2:9
enter
element 3:3
enter
element 4:1
enter
element 5:7
unsorted
list: 6 9
3 1 7
sorted
list:
1 3 6
7 9
No comments:
Post a Comment