Hey guys, what is up. In this particular post, we will be discussing what is merging. How we can concatenate/ merge two sorted arrays and two unsorted arrays.

What is merging?

The word, merge means to combine anything. In arrays, it is used to combine two arrays. Merging can take place between two unsorted arrays, one sorted and one unsorted array and two sorted arrays. Now three conditions arise that says that:

  • Merge two arrays in which the final array should be in an unsorted manner.
  • Combine one sorted and one unsorted array resulting array is also unsorted.
  • Concatenate two unsorted array and the resulting array should be in a sorted manner.
  • Merge two sorted arrays and the resulting array should be in a sorted array.

To deal with these we will be discussing all these points. Let’s start.

Merge two unsorted arrays:

When we want to combine two arrays in an unsorted manner irrespective of whether both arrays are unsorted or sorted and the resulting array first contains array one followed by array two. We will use this method only. In this, we will use three arrays. First of all, we will put array one in array three. Then we will put the values of the array second after the first array by starting the values from m. m minus one will be the last location where array one is stored.

ALGORITHM:

Step 1: i=0

Stair 2: Repeat steps from 3 to 4 using while loop i<m

Pass 3: C[i]=A[i]

Step 4: i++

Stair 5: i=0

            { END OF LOOP }

Pass 6: Repeat step from 7 to 8 using while loop i<n

Step 7: C[m]=B[i]

Stair 8: i++, m++

            { END OF LOOP }

Pass 9: STOP

merge two unsorted arrays output

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main()

{

            clrscr();

            const max=20;

            int A[max], B[max], C[max], i, m, n, p;

            cout<<“ENTER SIZE OF ARRAY 1= “;

            cin>>m;

            cout<<“ENTER SIZE OF ARRAY 2= “;

            cin>>n;

            p=n+m;

            cout<<endl<<“ENTER ELEMENTS OF BOTH THE ARRAYS IN ANY MANNER”;

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 1: “<<endl;

            for(i=0;i<m;i++)

            {              

                        cout<<“A[“<<i<<“]= “;

                        cin>>A[i];

            }

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 2: “<<endl;

            for(i=0;i<n;i++)

            {

                        cout<<“B[“<<i<<“]= “;

                        cin>>B[i];

            }

            i=0;

            while(i<m)

            {

                        C[i]=A[i];

                        i=i+1;

            }

            i=0;

            while(i<n)

            {

                        C[m]=B[i];

                        i=i+1;

                        m=m+1;

            }

            cout<<endl<<“FINAL ARRAY AFTER MERGING”<<endl;

            for(i=0;i<p;i++)

            {

                        cout<<“C[“<<i<<“]= “;

                        cout<<C[i]<<endl;

            }

            return 0;

}

Merge two sorted arrays and the final array is also sorted:

This can be done with two methods:

  1. First merge two arrays as it is in the third array. Later, we can do sorting in this using any of the sorting methods which include selection sort, bubble sort, and insertion sort.
  2. Secondly, , this can be done by adding elements in a sorted manner directly by checking this using loops and conditional statements.

We will see first method now:

Merge two sorted arrays output

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main()

{

            clrscr();

            const max=20;

            int A[max], B[max], C[max], i, m, n, p, j, temp;

            cout<<“ENTER SIZE OF ARRAY 1= “;

            cin>>m;

            cout<<“ENTER SIZE OF ARRAY 2= “;

            cin>>n;

            p=n+m;

            cout<<endl<<“ENTER ELEMENTS OF BOTH THE ARRAYS IN ASCENDING ORDER”;

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 1: “<<endl;

            for(i=0;i<m;i++)

            {

                        cout<<“A[“<<i<<“]= “;

                        cin>>A[i];

            }

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 2: “<<endl;

            for(i=0;i<n;i++)

            {

                        cout<<“B[“<<i<<“]= “;

                        cin>>B[i];

            }

            i=0;

            while(i<m)

            {

                        C[i]=A[i];

                        i=i+1;

            }

            i=0;

            while(i<n)

            {

                        C[m]=B[i];

                        i=i+1;

                        m=m+1;

            }

            i=1;

            while ( i< p)

            {

                        temp=C[i] , j=i-1;

                        while( j>=0 && C[j]>temp )

                        {

                                    C[j+1] = C[j];

                                    j=j-1;

                        }

                        C[j+1]=temp;

                        i=i+1;

            }

            cout<<endl<<“FINAL ARRAY AFTER MERGING”<<endl;

            for(i=0;i<p;i++)

            {

                        cout<<“C[“<<i<<“]= “;

                        cout<<C[i]<<endl;

            }

            return 0;

}

Now we will see second method in which we will concatenate two arrays and we will directly put arrays in sorted manner.

Merge two sorted arrays output 2

SOURCE CODE:

#include<iostream.h>

#include<conio.h>

int main()

{

            clrscr();

            const max=20;

            int A[max], B[max], C[max], i, m, n, p, j, k;

            cout<<“ENTER SIZE OF ARRAY 1= “;

            cin>>m;

            cout<<“ENTER SIZE OF ARRAY 2= “;

            cin>>n;

            p=n+m;

            cout<<endl<<“ENTER ELEMENTS OF BOTH THE ARRAYS IN ASCENDING ORDER”;

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 1: “<<endl;

            for(i=0;i<m;i++)

            {

                        cout<<“A[“<<i<<“]= “;

                        cin>>A[i];

            }

            cout<<endl<<“ENTER ELEMENTS OF ARRAY 2: “<<endl;

            for(i=0;i<n;i++)

            {

                        cout<<“B[“<<i<<“]= “;

                        cin>>B[i];

            }

            for(i=0,j=0,k=0;k<p;k++)

            {

                        if(i<n && j<m)

                        {

                                    if( A[i]>B[j] )

                                    {

                                                C[k]=B[j];

                                                j++;

                                    }

                                    else if( B[j]>A[i])

                                    {

                                                C[k]=A[i];

                                                i++;

                                    }

                                    else

                                    {

                                                C[k]=A[i];

                                                i++;

                                    }

                        }

                        else if(i==n && j<m )

                        {

                                    C[k]=B[j];

                                    j++;

                        }

                        else if(i<n && j==m )

                        {

                                    C[k]=A[i];

                                    i++;

                        }

            }

            cout<<endl<<“FINAL ARRAY AFTER MERGING”<<endl;

            for(i=0;i<p;i++)

            {

                        cout<<“C[“<<i<<“]= “;

                        cout<<C[i]<<endl;

            }

            return 0;

}

In the upcoming post, you will be getting information related to the working of any of the sensors. If you haven’t read our recent post on insertion and deletion in one-dimensional array then go and read this. Also, stay tuned for the upcoming posts.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *