#include<bits/stdc++.h>
using namespace std;
int main()
{
int i, j, k, n, m, p;
cout << "Enter First Array Element Number :: ";
cin >> n;
int a[n];
cout << "Enter First Array Element :: ";
for(i=0;i<n;i++)
{
cin >> a[i];
}
cout << endl << "Enter Second Array Element Number :: ";
cin >> m;
int b[m];
cout << "Enter Second Array Element :: ";
for(i=0;i<m;i++)
{
cin >> b[i];
}
int c[n+m];
i = j = k = 0;
while(i<n && j<m)
{
if(a[i]>b[j])
c[k++] = b[j++];
else if(a[i] < b[j])
c[k++] = a[i++];
else if(a[i] == b[j])
{
c[k++] = a[i++];
c[k++] = b[j++];
}
}
while(i<n)
c[k++] = a[i++];
while(j<m)
c[k++] = b[j++];
cout << endl << "After Merge Sort :: ";
for(i=0;i<m+n;i++)
{
cout << c[i] << " ";
}
cout << endl;
return 0;
}
using namespace std;
int main()
{
int i, j, k, n, m, p;
cout << "Enter First Array Element Number :: ";
cin >> n;
int a[n];
cout << "Enter First Array Element :: ";
for(i=0;i<n;i++)
{
cin >> a[i];
}
cout << endl << "Enter Second Array Element Number :: ";
cin >> m;
int b[m];
cout << "Enter Second Array Element :: ";
for(i=0;i<m;i++)
{
cin >> b[i];
}
int c[n+m];
i = j = k = 0;
while(i<n && j<m)
{
if(a[i]>b[j])
c[k++] = b[j++];
else if(a[i] < b[j])
c[k++] = a[i++];
else if(a[i] == b[j])
{
c[k++] = a[i++];
c[k++] = b[j++];
}
}
while(i<n)
c[k++] = a[i++];
while(j<m)
c[k++] = b[j++];
cout << endl << "After Merge Sort :: ";
for(i=0;i<m+n;i++)
{
cout << c[i] << " ";
}
cout << endl;
return 0;
}
Comments
Post a Comment