-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge.txt
108 lines (89 loc) · 1.75 KB
/
merge.txt
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define vi vector<int>
#define vll vector<long long>
#define mp make_pair
#define MPII map<int,int>
#define MPLL map<long long,long long>
#define MPSI map<string,int>
#define MPIS map<int,string>
#define PRII pair<int,int>
#define PRLL pair<long long,long long>
#define SP cout<<fixed<<setprecision(10);
#define rep(i,inval,n) for(int i = inval; i < n; i++)
#define all(x) x.begin(),x.end()
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
void Merge(int arr[],int left,int right,int mid)
{
int i,j,k;
int n1=mid-left+1;
int n2=right-mid;
int L[n1],R[n2];
for( i=0;i<n1;i++)
{
L[i]=arr[i+left];
}
for( i=0;i<n2;i++)
{
R[i]=arr[i+mid+1];
}
i=0;j=0;k=left;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void merge_sort(int arr[],int left,int right)
{
if(left<right)
{
int mid=left+(right-left)/2;
merge_sort(arr,left,mid);
merge_sort(arr,mid+1,right);
Merge(arr,left,right,mid);
}
}
int main()
{
IOS;
int t=1;
// cin>>t;
while(t--)
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++) cin>>arr[i];
merge_sort(arr,0,n-1);
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
cout<<endl;
}
return 0;
}