-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.c
100 lines (93 loc) · 2.26 KB
/
MergeSort.c
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
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define N 30
#define M 1000
int A[N], opc, i, j, aux, p_max, x;
void merge(left, middle, right){
int i,j,k;
i = left;
j = left;
k = middle + 1;
int tempA[N];
while(j<=middle && k<=right){
if(A[j]<A[k]){
tempA[i]=A[j];
i++;
j++;
}
else{
tempA[i]=A[k];
i++;
k++;
}
}
while(j<=middle){
tempA[i]=A[j];
i++;
j++;
}
for(i=left;i<k;i++)
A[i]=tempA[i];
}
void sort(left,right){
int middle;
if(left<right){
middle = (left + right)/2;
sort(left,middle);
sort(middle + 1,right);
merge(left,middle,right);
}
}
int main(){
while(opc != 4){
printf("******MENU*******\n\n1 - Vetor Ordenado\n2 - Vetor Aleatorio\n3 - Vetor Ordem Inversa\n4 - Sair \n\nOpcao: ");
scanf("%d", &opc);
switch(opc)
{
case 1:{
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = i;
printf("%d \n", A[i]);
}
sort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++){
printf("%d \n", A[i]);
}
break;
}
case 2:{
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = rand() % M;
printf("%d \n", A[i]);
}
sort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++){
printf("%d \n", A[i]);
}
break;
}
case 3:{
x = N;
printf("\nVETOR ORIGINAL\n");
for(i=0; i<N; i++){
A[i] = x;
x--;
printf("%d \n", A[i]);
}
sort(0, N-1);
printf("\nVETOR ORDENADO\n");
for(i=0; i<N; i++)
{
printf("%d \n", A[i]);
}
break;
}
}
getch();
}
}