forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge.cpp
76 lines (70 loc) · 1.27 KB
/
merge.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
73
74
75
76
#include <bits/stdc++.h>
using namespace std;
void display(vector<int> A, int n)
{
cout << endl;
for (auto i : A)
cout << i << " ";
}
void merge(int f, int mid, int l, vector<int> &A){
vector<int> temp;
int i=f,j=mid+1;
while(i<=mid && j<=l)
{
if(A[i]<A[j])
{
temp.push_back(A[i]);
i++;
}
else if(A[i]>A[j])
{
temp.push_back(A[j]);
j++;
}
else
{
temp.push_back(A[i]);
temp.push_back(A[j]);
j++;
i++;
}
}
while(i<=mid)
{
temp.push_back(A[i]);
i++;
}
while(j<=l)
{
temp.push_back(A[j]);
j++;
}
for(auto i:temp){
A[f]=i;
f++;
}
}
void M_S(vector<int> &A, int n,int f,int l)
{
if(f>=l)
return;
int mid=(f+l)/2;
M_S(A,n,f,mid);
M_S(A,n,mid+1,l);
merge(f,mid,l,A);
display(A, n);
}
int main()
{
vector<int> V;
int ch, n;
cout << "size: ";
cin >> n;
cout << "ENTER ARRY: ";
for (int i = 0; i < n; i++)
{
cin >> ch;
V.push_back(ch);
}
M_S(V, n, 0, n-1);
}