2
/*Program to merge to arrays using pointers in descending order, when the first array is in ascending order 

and the second array is in descending order*/

#include<iostream.h>
#include<conio.h>

void merge(int *ptr1, int *ptr2, int m, int n)
{
    int *p=new int[m+n],i,j,k;
    for(i=0,k=m-1;i<(m/2);i++,k--) //to reverse the fir``st array from ascending to descending
    {
       j=*(ptr1+i);
       *(ptr1+i)=*(ptr1+k);
       *(ptr1+k)=j;
    }
    for(i=0,j=0,k=0;i<m&&j<n;)
    {
       if (*(ptr1+i) > *(ptr2+j))
       {
      *(p+k)=*(ptr1+i);
      i++;k++;
       }
       else
       {
          *(p+k)=*(ptr2+j);
      j++;k++;
       }
    }
    if(i==m)
       while(j<n)
       {
          *(p+k)=*(ptr2+j);
          j++;k++;
       }
    else if(j==n)
       while(i<m)
       {
      *(p+k)=*(ptr1+i);
      i++;k++;
       }
    cout<<"\n\n";
    for(i=0;i<k;i++)
       cout<<*(p+i)<<" ";
    getch();
    delete p;
}

void  main()
{
   clrscr();
   int i,j,k,a,b;
   cout<<"\nEnter the size of the first array first array : ";
   cin>>a;
   cout<<"\nEnter the size of second array : ";
   cin>>b;
   int *p1=new int[a], *p2=new int[b];
   cout<<"\nEnter the elements of the first array : ";
   for(i=0;i<a;i++)
      cin>>*(p1+i);
   cout<<"\nEnter the elements of the second array : ";
   for(i=0;i<b;i++)
      cin>>*(p2+i);
   for(i=1;i<a;i++) //insertion sort to sort 1st array in ascending order
   {
      k=*(p1+i);
      for(j=i-1;j>=0&&*(p1+j)>k;j--)
     *(p1+j+1)=*(p1+j);
      *(p1+j+1)=*(p1+j);
   }
   for(i=1;i<b;i++) //insertion sort to sort the 2nd array in descending order
   {
      k=*(p2+i);
      for(j=i-1;j>=0&&*(p2+j)>k;j--)
     *(p2+j+1)=*(p2+j);
      *(p2+j+1)=*(p2+j);
   }
   for(i=0;i<k;i++)
       cout<<*(p1+i)<<" ";
   cout<<endl;
  /* int c[]={3,5,7};
   int d[]={8,6,4};
   merge(c,d,3,3); */ To check
   merge(p1,p2,a,b);
  delete p1;
  delete p2;
}

The merge function is working perfect, but I don't understand why the insertion sort is not working propoerly. Can anyone help?

I checked the merge separately by using sorted static array, as shown at the end of the code, the the merging worked fine, but when I used dynamic allocation, the code did not work.

6
  • 3
    Why dont you use STL algorithms? That would be a 5 liner. Commented Jan 8, 2012 at 20:56
  • 1
    void main() is not valid C++. (And I have doubts about <conio.h>, too.) Commented Jan 8, 2012 at 20:59
  • I believe <conio.h> is a Win32ism. Commented Jan 8, 2012 at 21:04
  • conio is just a library (originally from Borland [I think], but since ported widely). It's not standard, but it's far from being invalid. Commented Jan 8, 2012 at 21:22
  • 1
    STL would make this task significantly easier. Commented Jan 8, 2012 at 22:39

2 Answers 2

1
for(j=i-1;j>=0&&*(p1+j)>k;j--)
    *(p1+j+1)=*(p1+j);
*(p1+j+1)=*(p1+j);

The last line should read

*(p1+j+1)=k

otherwise you'll get some bogus data since j == -1 after the loop. And the same for the second sort.

Furthermore, your printout of the array at the end is wrong, it should use a, not k as upper limit.

Your merge function reverses one array, but not the other. Either you have to sort the second array in descending order (as the comment says it should), or better yet, remove the reversing in the beginning of the merge function and sort the arrays in the correct order to begin with.

And lastly, your code gets a lot easier to read (and therefore to debug) if you use descriptive variable names and indent the code correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

yeah, now i see, i should have noticed it. A silly mistake it ws! thanks :)
1

You should also replace delete with delete[].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.