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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "util.h"
typedef int T;
// https://stackoverflow.com/a/22900767
#define LEFT(idx) (idx*2+1)
#define RIGHT(idx) (idx*2+2)
#define PARENT(idx) ((idx-1)/2)
static void
swap(T heap[], int i, int j)
{
T temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
static void
fixup(T heap[], int i)
{
int p = PARENT(i);
while ( i > 0 && heap[p] < heap[i] ) {
swap(heap, i, p);
i = p;
p = PARENT(i);
}
}
static void
fixdown(T heap[], int i, int n)
{
for ( ;; ) {
const int l = LEFT(i);
const int r = RIGHT(i);
int m = i;
if ( l < n && heap[m] < heap[l] )
m = l;
if ( r < n && heap[m] < heap[r] )
m = r;
if ( m == i )
break;
swap(heap, m, i);
i = m;
}
}
static void
heapify(T heap[], int n)
{
for ( int i = n / 2 - 1; i >= 0; --i )
fixdown(heap, i, n);
}
static void
my_heapsort(T a[], int n)
{
heapify(a, n);
for ( int i = n - 1; i >= 0; --i ) {
swap(a, 0, i);
fixdown(a, 0, i);
}
}
// -------------------------------------------
struct pq { // Priority Queue
T heap[251];
int sz;
};
void
pq_init(struct pq *pq)
{
pq->sz = 0;
}
bool
pq_push(struct pq *pq, T data)
{
if ( pq->sz == NELEM(pq->heap) )
return false;
pq->heap[pq->sz] = data;
fixup(pq->heap, pq->sz);
++pq->sz;
return true;
}
bool
pq_pop(struct pq *pq, T* data)
{
if ( pq->sz == 0 )
return false;
*data = pq->heap[0];
--pq->sz;
pq->heap[0] = pq->heap[pq->sz];
fixdown(pq->heap, 0, pq->sz);
return true;
}
// -------------------------------------------
void print_heap(T heap[], int n)
{
if ( n ) {
printf("%d", heap[0]);
for ( int i = 1; i != n; ++i )
printf(", %d", heap[i]);
putchar('\n');
}
}
int main(void)
{
#if 0 // Heap-Testprogramm
T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 };
print_heap(heap, 10);
//heap[10] = 13; fixup(heap, 10);
swap(heap, 0, 9); fixdown(heap, 0, 9);
print_heap(heap, 9);
#endif
struct pq pq[1];
pq_init(pq);
for ( int i = 0; i != 10; ++i )
pq_push(pq, rand());
T data;
while ( pq_pop(pq, &data) )
printf("%d\n", data);
}
|