-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuiuiuiuiuiuiuiuiui.txt
59 lines (53 loc) · 1.36 KB
/
uiuiuiuiuiuiuiuiui.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
#include <stdio.h>
#include <stdlib.h>
#define M 100000
#define less(A, B) (A < B)
void armazena(int *vetor){
int i = 0;
if(i > M-1) return;
scanf(" %d", &vetor[i]);
i++;
}
void imprime(int *vetor){
int quantidade;
scanf(" %d", &quantidade);
mergeSort(vetor, 0, M-1);
for(int i = 0; i < quantidade; i++)
printf("%d ", vetor[i]);
}
void mergeSort(int *vetor, int l, int r){
if(l >= r) return;
int meio = (r-l/2)+l;
mergeSort(vetor, l, meio);
mergeSort(vetor, meio+1, r);
merge(vetor, l, meio, r);
}
void merge(int *vetor, int l, int meio, int r){
int l1 = l, r1 = meio, l1i = l1;
int l2 = meio + 1, r2 = r, l2i = l2;
int *c = malloc(sizeof(int) * (r-l+1));
int lc = 0, rc = r-l, ci = 0;
while(l1i <= r1 && l2i <= r2){
if(less(vetor[l1i], vetor[l2i])){
c[ci++] = vetor[l1i++];
}else{
c[ci++] = vetor[l2i++];
}
}
while(l1i <= r1)
c[ci++] = vetor[l1i++];
while(l2i <= r2)
c[ci++] = vetor[l2i++];
l1i = l;
for(ci = 0; ci <= rc; ci++)
vetor[l1i++] = c[ci];
free(c);
}
int main (void){
int *vetor = malloc(M*sizeof(int));
int n;
while(scanf(" %d", &n) != 1){
if(n == 1) armazena(vetor);
else if(n == 2) imprime(vetor);
}
}