forked from Vishal-Aggarwal0305/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesortarray.cpp
60 lines (47 loc) · 1.03 KB
/
mergesortarray.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<iostream>
#include<stdio.h>
using namespace std;
void Merge(int A[50],int B[50],int p, int q)
{
int i = 0, j = 0, k = 0, C[50];
while (i<p && j <q)
{
if (A[i] < B[j])
C[k++] = A[i++];
else if(A[i]==B[j])
{
C[k++]=A[i++];
C[k++]=B[j++];
}
else
C[k++] = B[j++];
}
while (i < p)
C[k++] = A[i++];
while (j < q)
C[k++] = B[j++];
cout<<"\nMerged array is as follows................."<<endl;
for(i=0;i<p+q;i++)
cout<<C[i]<<"\t";
}
int main()
{
int m,n, i;
cout<<"\nEnter the size of array 1"<<endl;
cin>>m;
cout<<"\nEnter the size of array 2"<<endl;
cin>>n;
int arr1[m],arr[n];
cout<<"\nEnter element of array 1"<<endl;
for(i = 0; i < m; i++)
{
cin>>arr1[i];
}
cout<<"\nEnter element of array 2"<<endl;
for(i = 0; i < n; i++)
{
cin>>arr[i];
}
Merge(arr1,arr,m,n);
return 0;
}