12. Write a function to merge two sorted array in sorted manner

#include<iostream>
using namespace std;

void mergearray(int a1[], int a2[], int s1, int s2)
{
    int a3[s1+s2],i=0,j=0,k=0;

    while(i<s1 && j<s2)
    {
        if(a1[i]<a2[j])
            a3[k++] = a1[i++];
        else
            a3[k++] = a2[j++];

    }
    while(i<s1)
    {
        a3[k++] = a1[i++];
    }
    while(j<s2)
    {
        a3[k++] = a2[j++];
    }

    for(int i = 0;i<s1+s2;i++)
        cout<<a3[i]<<"\t";

}
int main()
{
    int arr1[5]={1,4,26,55,77}, arr2[5]={2,4,17,80,99};
    mergearray(arr1,arr2,5,5);

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s