-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.cpp
More file actions
42 lines (36 loc) · 712 Bytes
/
Copy pathmergeSort.cpp
File metadata and controls
42 lines (36 loc) · 712 Bytes
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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const int N=1e5+10,M=1e5+10,INF=0x3f3f3f3f,MOD=1e9+7;
const double EPS=1e-8;
int n,m;
int a[N],b[N];
void mergeSort(int s[],int l,int r){
if(l==r)return;
int m=l+(r-l)/2;
mergeSort(s,l,m);
mergeSort(s,m+1,r);
int k=0,i=l,j=m+1;
while(i<=m&&j<=r){
if(s[i]<=s[j])b[k++]=s[i++];
else b[k++]=s[j++];
}
while(i<=m){
b[k++]=s[i++];
}
while(j<=r){
b[k++]=s[j++];
}
for(int i=l,j=0;i<=r;i++,j++){
s[i]=b[j];
}
}
int main(){
scanf("%d",n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
mergeSort(a,0,n-1);
}