-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sort.cpp
More file actions
55 lines (50 loc) · 882 Bytes
/
merge-sort.cpp
File metadata and controls
55 lines (50 loc) · 882 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
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int v[N],aux[N];
int n;
void merge_sort(int ini,int fim)
{
if (ini==fim)
return;
int tam=0;
int i,j=(ini+fim)/2+1;
merge_sort(ini,(ini+fim)/2);
merge_sort((ini+fim)/2+1,fim);
for (i=ini; i<=(ini+fim)/2; i++)
{
while(j<=fim && v[j]<v[i])
{
aux[tam]=v[j];
tam++;
j++;
}
aux[tam]=v[i];
tam++;
}
while(j<=fim)
{
aux[tam]=v[j];
tam++;
j++;
}
for (i=ini; i<=fim; i++)
v[i]=aux[i-ini];
}
int main()
{
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
scanf("%d", &v[i]);
}
merge_sort(1,n);
for (int i=1; i<=n; i++)
{
printf("%d ", v[i]);
}
printf("\n");
return 0;
}