-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path힙과힙정렬_1.c
98 lines (83 loc) · 1.34 KB
/
힙과힙정렬_1.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 <string.h>
#pragma warning(disable : 4996)
#define SIZE 100
typedef struct {
int N;
int H[SIZE];
}Heap;
void init(Heap *heap) {
heap->N = 0;
return;
}
void upHeap(Heap* heap) {
int K = heap->H[heap->N];
int i = heap->N;
while (i != 1 && heap->H[i / 2] < K) {
heap->H[i] = heap->H[i / 2];
i = i / 2;
}
heap->H[i] = K;
return;
}
void downHeap(Heap* heap) {
int K = heap->H[1];
int p = 1, c = 2;
while (c <= heap->N) {
if (c < heap->N && heap->H[c] < heap->H[c + 1])
c++;
if (K > heap->H[c])
break;
heap->H[p] = heap->H[c];
p = c;
c = c * 2;
}
heap->H[p] = K;
return;
}
void insertItem(Heap* heap, int K) {
heap->N++;
heap->H[heap->N] = K;
upHeap(heap);
return;
}
int removeMax(Heap* heap) {
int K = heap->H[1];
heap->H[1] = heap->H[heap->N];
heap->N--;
downHeap(heap);
return K;
}
void printHeap(Heap* heap) {
for (int i = 1; i <= heap->N; i++)
printf(" %d", heap->H[i]);
printf("\n");
}
int main() {
Heap heap;
init(&heap);
int num;
char order;
while (1) {
scanf("%c", &order);
switch (order) {
case 'i':
scanf("%d", &num);
insertItem(&heap, num);
printf("0\n");
break;
case 'd':
printf("%d\n", removeMax(&heap));
break;
case 'p':
printHeap(&heap);
break;
case 'q':
return 0;
default :
break;
}
}
return 0;
}