Write a program to sort two arrays and combine them into one array using mergesort

Source Code

#include<stdio.h>
#include<conio.h>
int main (int argc, char *argv[])
{
  clrscr();
  int ar[100];
  int ar2[100];
  int arm[100];
  int r1,r2,r,i,j,f=0;
  for(i=0;i<100;i++)
  {
    ar[i]=0;
    ar2[i]=0;
    arm[i]=0;
  }
  printf("\n Enter the range of two arrays:(less than 100) ");
  scanf("%d %d",&r1,&r2);
  printf("\n Enter the elements of the first array:");
  for(i=0;i<r1;i++)
  {
       printf("\n Enter element number: %d ",(i+1));
       scanf("%d",&ar[i]);
  }
  printf("\n Enter the elements of the second array:");
  for(i=0;i<r2;i++)
  {
       printf("\n Enter element number: %d ",(i+1));
       scanf("%d",&ar2[i]);
  }
  r=r1+r2;
  for(i=0;i<r1;i++)
  {
    arm[i]=ar[i];
  }
  for(i=r1;i<r;i++)
  {
         for(j=0;j<i;j++)
         {
      if(ar2[i-r1]==arm[j])
      {
        f=1;
        break;
      }
         }
         if(f==0)
       arm[i]=ar2[i-r1];
  }
  printf("\n The elements of merged array is:");
  for(i=0;i<r;i++)
  {
    printf("%d ",arm[i]);
  }
  getch();
}

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code