Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7042565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:11:02+00:00 2026-05-28T02:11:02+00:00

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

  • 0
/*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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:11:02+00:00Added an answer on May 28, 2026 at 2:11 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Program overrides public void paint(Graphics g, int x, int y); in order to
Simply put this program is merging two arrays containing coordinates into one larger array,
I have a winform program that uses Merge Replication to keep a local SQL
If I have the following arrays arry1 = array( 101 => array( 'title1' =>
How do I configure Apple's FileMerge program to function as Mercurial's merge tool? I
I want to create a PHP recursive program using a Binary Tree and Recursion.
What are the arguments to put in eclipse subversion merge program arguments to make
Is there some merge strategy or program which is aware of key-value stores, in
This is a basic merge sort program: The problem is when I try to
Ghostscript merge of pdf's is causing orientation to flip I'm using a similar method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.