#include< stdio.h>
#include< conio.h>
void mergesort(int a[],int n)
{
int b[50],c,low1,high1,high2,low2;
int i,k,j;
c=1;
while(c< n)
{
low1=0;
k=0;
while(low1+c< n)
{
low2=low1+c ;
high1=low2-1;
if(low2+c-1< n)
high2=low2+c-1;
else
high2=n-1;
i=low1;
j=low2;
while(i< =high1 && j< =high2)
{
if(a[i]< =a[j])
b[k++] =a[i++];
else
b[k++] = a[j++];
}
while(i< =high1)
b[k++]=a[i++];
while(j< =high2)
b[k++] =a[j++];
low1=high2+1;
}
i=low1;
while(k< n)
b[k++] =a[i++];
for(i=0;i< n;i++)
a[i]=b[i];
c=c*2;
}
}
main()
{
int a[20],i,n;
clrscr();
printf("Enter The number Of Elements\t: ");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}
printf("\nArray Befor Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
mergesort(a,n);
printf("\nArray After Sorting : ");
for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}
/* OUTPUT
Enter The number Of Elements : 10
Element 1 : 12
Element 2 : 54
Element 3 : 98
Element 4 : 6566
Element 5 : 45
Element 6 : 12
Element 7 : 5
Element 8 : 1
Element 9 : 156
Element 10 : 21
Array Befor Sorting : 12 54 98 6566 45 12 5 1 156 21
Array After Sorting : 1 5 12 12 21 45 54 98 156 6566
*/
Wednesday, February 27, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
I am soo glad i got this. Thank you for the program. You are a lyf-saver
Post a Comment