-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.cpp
70 lines (54 loc) · 1.35 KB
/
mergesort.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
#include <bits/stdc++.h>
using namespace std;
// saido direto da wikipedia
void merge(int *saida, int *auxiliar, int inicio, int meio, int fim){
int i, j, k;
i = inicio;
j = meio + 1;
k = inicio;
while(i <= meio && j <= fim){
if(auxiliar[i] < auxiliar[j]){
saida[k] = auxiliar[i];
i++;
}
else{
saida[k] = auxiliar[j];
j++;
}
k++;
}
while(i <= meio){
saida[k] = auxiliar[i];
i++;
k++;
}
while(j <= fim){
saida[k] = auxiliar[j];
j++;
k++;
}
//Copia os elementos que foram ordenados para o auxiliar
for(int p = inicio; p <= fim; p++)
auxiliar[p] = saida [p];
}
void mergeSort(int *saida, int *auxiliar, int inicio, int fim){
if(inicio < fim){
int meio = (inicio + fim) / 2;
mergeSort(saida, auxiliar, inicio, meio);
mergeSort(saida, auxiliar, meio + 1, fim);
merge(saida, auxiliar, inicio, meio, fim);
}
}
int main()
{
int n; vector<int> a;
while(cin >> n) a.push_back(n);
int v[a.size()]; int ans[a.size()];
for(int i=0; i<a.size(); i++) v[i] = a[i];
mergeSort(ans, v, 0, a.size()-1);
for(int i=0; i<a.size(); i++)
{
cout << ans[i] << " ";
} cout << "\n";
return 0;
}