Friday, October 2, 2009

ADA 5th sem lab prgms

Prgm no.3
/*Sort a given set of elements using the Merge Sort method..*/
#include
#include
int num[10], x[10];
void algorithm(int small, int s, int big)
{
int m,n,p;
p=small;
m=small;
n=s+1;
while((m<=s)&&(n<=big))
{
if(x[m] num[p++]=x[m++];
else
num[p++]=x[n++];
}
while(m<=s)
num[p++]=x[m++];
while(n<=big)
num[p++]=x[n++];
for(m=small;m<=p-1;m++)
x[m]=num[m];
}
void sort(int small,int big)
{
int s;
if(small {
s=(small+big)/2;
sort(small,s);
sort(s+1,big);
algorithm(small,s,big);
}
}
void main()
{
int i,n;
clrscr();
printf("Enter the number of Elements\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for(i=0;i scanf("%d",&x[i]);
sort(0,n-1);
printf("The Sorted Element are\n");
for(i=0;i printf("%d\n",num[i]);
getche();
}

/*
Output:
Enter the number of Elements
5
Enter the Elements
20
10
50
40
30
The Sorted Element are
10
20
30
40
50
*/

No comments:

Post a Comment