-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTwoSortedArrays.cpp
More file actions
63 lines (55 loc) · 1.2 KB
/
Copy pathTwoSortedArrays.cpp
File metadata and controls
63 lines (55 loc) · 1.2 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
57
58
59
60
61
62
63
//Program to sort two arrays in O(1) space
#include <bits/stdc++.h>
using namespace std;
int divide(int k){
if(k<=1)
return 0;
return (k/2)+(k%2);
}
void twoSortedArrays(vector<int>&arr1,vector<int>&arr2,int m,int n){
int gap=m+n;
int i,j;
for(gap = divide(gap);gap>0;gap=divide(gap)){
for(i=0;gap+i<m;i++){
if(arr1[i]>arr1[i+gap])
swap(arr1[i],arr1[gap+i]);
}
for(j = gap>m ? gap-m :0;i<m && j<n;i++,j++){
if(arr1[i]>arr2[j])
swap(arr1[i],arr2[j]);
}
if(j<n){
for(j=0;gap+j<n;j++){
if(arr2[j]>arr2[j+gap])
swap(arr2[j],arr2[j+gap]);
}
}
}
}
int main()
{
int temp;
int m,n;
cin>>m;
vector<int>arr1;
vector<int>arr2;
for(int i=0;i<m;i++){
cin>>temp;
arr1.push_back(temp);
}
cin>>n;
for(int j=0;j<n;j++){
cin>>temp;
arr2.push_back(temp);
}
twoSortedArrays(arr1,arr2,m,n);
for(int i=0;i<m;i++){
cout<<arr1[i]<<" ";
}
cout<<endl;
for(int j=0;j<n;j++){
cout<<arr2[j]<<" ";
}
cout<<endl;
return 0;
}