Welcome To Uday Satya Blog

24 . MERGE SORT



/* ***************************************************
     24. TO SEARCH ELEMENTS USING MERGE SORT .
  ************************************************** */
#include<*stdio.h>
#include<*conio.h>
void msort(int a[],int b[],int c[],int n1,int n2,int n3);
int a[50],b[50],c[50],n1,n2,n3,i;
void main()
{
printf("Enter size of a\n");
scanf("%d",&n1);
printf("Enter the elements of a");
for(i=0;i
    scanf("%d",&a[i]);
printf("Enter size of b\n");
scanf("%d",&n2);
printf("Enter the elements of b");
for(i=0;i
    scanf("%d",&b[i]);
n3=n1+n2;
msort(a,b,c,n1,n2,n3);
printf("Elements after sorting are:");
for(i=0;i
    printf("%2d\n",c[i]);
getch();
}

void msort(int a[],int b[],int c[],int n1,int n2,int n3)
{
int apoint,bpoint,cpoint,alimit,blimit,climit;
apoint=0;bpoint=0;
alimit=n1-1;blimit=n2-1;
if(n1+n2 != n3)
    printf("Array bounds Incompatable");
for(cpoint=0;apoint<=alimit&&bpoint<=blimit;cpoint++)
    {
    if(a[apoint]
        c[cpoint]=a[apoint++];
    else
        c[cpoint]=b[bpoint++];
    }
while(apoint<=alimit)
    c[cpoint++]=a[apoint++];
while(bpoint<=blimit)
    c[cpoint++]=b[bpoint++];
}

 output

 Enter Size of a : 4

enter the elements of a : 1  4  5  7

enter size of b : 4

Enter the elements of b : 2  3  6  8

elements after sorting :  1   2   3   4  5  6  7  8

0 comments:

Post a Comment