-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_two_arrays_sorted_order.cpp
More file actions
69 lines (52 loc) · 1.17 KB
/
merge_two_arrays_sorted_order.cpp
File metadata and controls
69 lines (52 loc) · 1.17 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
64
65
66
67
68
69
#include<iostream>
using namespace std;
void bubble_sort(int arr[], int n){
for(int i = 0; i<n-1; i++){
for(int j = 0; j<n-i-1; j++){
if(arr[j] > arr[j+1]){
swap(arr[j],arr[j+1]);
}
}
}
}
int main(){
int n;
cout << "Enter the size for Array 1: ";
cin >> n;
int arr[n];
cout << "Taking input into the array 1 : ";
for(int i = 0; i<n; i++){
cin >> arr[i];
}
int m;
cout << "Enter the size for Array 2: ";
cin >> m;
int crr[m];
cout << "Taking input into the array 2 : ";
for(int j = 0; j<m; j++){
cin >> crr[j];
}
bubble_sort(arr,n);
bubble_sort(crr,m);
int trr[m+n];
int k = 0,i = 0,j = 0;
while(i<n && j<m){
if(arr[i] >= crr[j]){
trr[k++] = crr[j++];
}
else if(arr[i] <= crr[j]){
trr[k++] = arr[i++];
}
}
while(i < n){
trr[k++] = arr[i++];
}
while(j < m){
trr[k++] = crr[j++];
}
cout << "Printing the Merged Sorted Array : ";
for(int l = 0; l<(m+n); l++){
cout << trr[l] << " ";
}
return 0;
}