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

Archives

program of merge sort

Tuesday, September 1, 2009

void main()
{
 clrscr();
 int a[10],b[10],c[20],m,n,p,j;
 cout<<"enter the number of elements of first array: ";
 cin>>m;
 cout<<"enter the elements of first array"<<endl;
 for(int i=0;i<m;i++)
 cin>>a[i];
 cout<<"enter the number of elements of second array: ";
 cin>>n;
 cout<<"enter the elements of second array:"<<endl;
 for(i=0;i<n;i++)
 cin>>b[i];
 i=j=p=0;
 for(int l=0;l<m+n;l++)
 {
  if(i==m||j==n)
  { if(m<n)
    {for(int s=j;s<m+n;s++)
      {c[p]=b[j];
       p++;
       j++;}
     }
     else
     { for(int s=i;s<m+n;s++)
       {c[p]=a[i];
    p++;
    i++;}
      }
   }
  if(a[i]<b[j])
   { c[p]=a[i];
     p++;
     i++;
   }
   else
   {
    c[p]=b[j];
    p++;
    j++;
   }
  }
  cout<<"elements after merging";
  for(i=0;i<m+n;i++)
  cout<<c[i]<<" ";
  getch();
  }

0 comments: