Merge Operation on Array


This operation is used to merge two one dimensional array into third one dimensional array.
In order to merge two one dimensional array first we have to copy all the elements of first one dimensional array into third one dimensional array and then copy all the elements of second one dimensional array into third one dimensional array.

Algorithm For Merge Operation in Array


Step 1: I = 0
             J = 0
           K = 0
Step 2: Repeat Step3 while I < N1
Step 3: C [K] = A [I]
             K = K + 1
             I = I + 1
Step 4: Repeat Step4 while J < N2
Step 5: C [K] = B [J]
            K = K + 1
            J = J + 1


     

Program For Merge Operation in Array


#include<stdio.h>
#include<conio.h>
#define N1 5
#define N2 5
#define N3 10
void main()
{
      int a[N1]={10,20,30,40,50};
      int b[N2]={15,25,35,45,55};
      int c[N3];
      void traverse(int *a,int n);
      void merge(int *a,int *b,int *c);
      clrscr();
      printf("Contents of A:\n");
      traverse(a,N1);
      printf("Contents of B:\n");
      traverse(b,N2);
      merge(a,b,c);
      printf("After Merging\n");
      traverse(c,N3);
      getch();
}
void merge(int *a,int *b,int *c)
{
int i=0,j=0,k=0;
while(i<N1)
{
      c[k]=a[i];
      k=k+1;
i=i+1;
}
while(j<N2)
{
c[k]=b[j];
k=k+1;
j=j+1;
}
}
void traverse(int *a,int n)
{
      int START=0;
      while(START<n)
      {
          printf("%d\n",a[START]);
          START=START+1;
      }
}

Download Projects


Download Programs