-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathmerge_without_extra_space.cpp
73 lines (67 loc) · 2.29 KB
/
merge_without_extra_space.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
61
62
63
64
65
66
67
68
69
70
71
72
// C++ Program to Merge two sorted arrays without using extra space
/* Given two sorted arrays, say of size m and n respectively
The merge function will merge these sorted arrays such that the first array has m smallest elements
and the second array has remaining n elements in a sorted manner.
We will simultaneously iterate through both the arrays
and compare the first element of first array with the last element of second array,
if element of second array is smaller than element of first array then swap it,
otherwise move on to the next element and repeat the previous step.
*/
#include <bits/stdc++.h>
using namespace std;
//Function to merge two sorted arrays
void merge(int arr1[], int arr2[], int n, int m) {
//start two pointers, one from first position of arr1 and another from last position of arr2
int i=n-1, j=0;
//Iterate through every element of arr1 and arr2 and compare elements of the arrays
while(i>=0 && j<m){
if(arr2[j]<arr1[i]){
swap(arr1[i], arr2[j]);
}
i--;
j++;
}
//sort the first array
sort(arr1, arr1+n);
//sort the second array
sort(arr2, arr2+m);
}
int main() {
cout<<"Enter the size of the first array : ";
int n, m, i;
cin >> n;
cout<<"Enter the size of the second array : ";
cin >> m;
int arr1[n], arr2[m];
cout<<"Enter the elements of the first array : ";
for (i = 0; i < n; i++) {
cin >> arr1[i];
}
cout<<"Enter the elements of the second array : ";
for (i = 0; i < m; i++) {
cin >> arr2[i];
}
//call the merge function on both the arrays
merge(arr1, arr2, n, m);
cout<<"Sorted Array : ";
for (i = 0; i < n; i++) {
cout << arr1[i] << " ";
}
for (i = 0; i < m; i++) {
cout << arr2[i] << " ";
}
cout << "\n";
return 0;
}
/*
SAMPLE INPUT AND OUTPUT
INPUT:
Enter the size of the first array : 4
Enter the size of the first array : 5
Enter the elements of the first array : 1 3 5 7
Enter the elements of the first array : 0 2 6 8 9
OUTPUT:
Sorted Array : 0 1 2 3 5 6 7 8 9
*/
//Time Complexity : O((n+m)log(n+m))
//Space Complexity : O(1)