-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sortd_Arrays.cpp
More file actions
56 lines (55 loc) · 1.16 KB
/
Merge_Sortd_Arrays.cpp
File metadata and controls
56 lines (55 loc) · 1.16 KB
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
#include <bits/stdc++.h>
using namespace std;
void printArray(int arr[], int n)
{
cout << endl;
int i = 0;
cout << "ARRAY THUS FORMED IS : ";
while (i < n)
{
cout << arr[i++] << " ";
}
}
//APPROACH 1ST
void merge_sorted_array(int arr1[], int arr2[], int n, int m)
{
int i = 0, j = 0;
for (int i = 0; i < n; i++)
{
if (arr1[i] > arr2[j])
{
swap(arr1[i], arr2[j]);
sort(arr2, arr2 + m);
}
}
}
//APPROACH 1ST
int main()
{
int n, i = 0, m;
cout << "Enter the number of Elements in First Sorted Array : ";
cin >> n;
int arr1[n];
cout << "Enter the Elements : ";
while (i < n)
{
cin >> arr1[i++];
}
cout << endl
<< "Enter the number of Elements in second Aray : ";
cin >> m;
i = 0;
int arr2[m];
cout << "Enter the Elements : ";
while (i < m)
{
cin >> arr2[i++];
}
printArray(arr1, n);
printArray(arr2, m);
merge_sorted_array(arr1, arr2, n, m);
cout << endl
<< "ARRAYS AFTER MERGE SORTING AT CONSTANT SPACE COMPLEXIVITY : " << endl;
printArray(arr1, n);
printArray(arr2, m);
}