-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.c
More file actions
94 lines (89 loc) · 2.12 KB
/
HeapSort.c
File metadata and controls
94 lines (89 loc) · 2.12 KB
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
#include<stdio.h>
#include<conio.h>
#define n 6
#define m 1000
int A[n], lenght, i, j, opc, x, aux;
void constroi_heap()
{
for(i=(n-1)/2; i>=0; i--){
descer(i, n-1);
}
}
void descer(i, lenght)
{
j = (i*2)+1;
while(j <= lenght){
if(j < lenght && A[j+1] > A[j])
j++;
if(A[i] < A[j]){
aux = A[i];
A[i] = A[j];
A[j] = aux;
i = j;
j =(i*2)+1;
}
else j = lenght+1;
}
}
void heap_sort()
{
constroi_heap ();
for(i=n-1; i >= 1; i--){
aux = A[0];
A[0] = A[i];
A[i] = aux;
descer (0, i-1);
}
}
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]);
}
heap_sort();
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]);
}
heap_sort();
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]);
}
heap_sort();
printf("\nVETOR ORDENADO\n");
for(i=0; i<n; i++)
{
printf("%d \n", A[i]);
}
break;
}
}
getch();
}
}