My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program of dynamic merge

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int n,m,*p,*q,*r;
  cout<<"enter the size of first array: ";
  cin>>n;
  p=new int[n];
  cout<<"enter the sorted elements of first array: ";
  for(int i=0;i<n;i++)
  cin>>*(p+i);
  cout<<"enter the size of second array: ";
  cin>>m;
  q=new int[m];
  cout<<"enter the sorted elements of second array: ";
  for(i=0;i<m;i++)
  cin>>*(q+i);
  r=new int[n+m];
  int a,b,c;
  a=b=c=0;
  for(i=0;i<n+m;i++)
  {  if(*(p+a)<*(q+b))
     {  *(r+c)=*(p+a);
    c++;
    a++;
    if(a==n)
    {  for(int j=b;j<m;j++)
       {  *(r+c)=*(q+b);
          c++;
          b++;
        }
        break;
    }
     }
     else
     {  *(r+c)=*(q+b);
    c++;
    b++;
    if(b==m)
    {  for(int j=a;j<n;j++)
       {  *(r+c)=*(p+a);
          c++;
          a++;
        }
        break;
    }
     }
  }
  cout<<"elements after merging is: ";
  for(i=0;i<m+n;i++)
  cout<<*(r+i)<<" ";
  getch();
  }

0 comments: