aboutsummaryrefslogtreecommitdiff
path: root/heap.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-11-27 16:52:44 +0100
committerThomas Schmucker <ts@its1.de>2020-11-27 16:52:44 +0100
commit110d55e652900cb5b146ae38933e928e96db4867 (patch)
tree445f00329ffe28ac43fea2115ddf1dd17b4c5d8d /heap.c
parent4d1d41985e1f3033ef82e9761b0bc49310008cf0 (diff)
downloaddata-structures-110d55e652900cb5b146ae38933e928e96db4867.tar.gz
data-structures-110d55e652900cb5b146ae38933e928e96db4867.tar.bz2
data-structures-110d55e652900cb5b146ae38933e928e96db4867.zip
reformat heap.c
Diffstat (limited to 'heap.c')
-rw-r--r--heap.c28
1 files changed, 14 insertions, 14 deletions
diff --git a/heap.c b/heap.c
index 365d925..7976e52 100644
--- a/heap.c
+++ b/heap.c
@@ -1,15 +1,16 @@
1#include <stdbool.h>
1#include <stdio.h> 2#include <stdio.h>
2#include <stdlib.h> 3#include <stdlib.h>
3#include <stdbool.h> 4
4#include "util.h" 5#include "util.h"
5 6
6typedef int T; 7typedef int T;
7 8
8// https://stackoverflow.com/a/22900767 9// https://stackoverflow.com/a/22900767
9 10
10#define LEFT(idx) (idx*2+1) 11#define LEFT(idx) (idx * 2 + 1)
11#define RIGHT(idx) (idx*2+2) 12#define RIGHT(idx) (idx * 2 + 2)
12#define PARENT(idx) ((idx-1)/2) 13#define PARENT(idx) ((idx - 1) / 2)
13 14
14static void 15static void
15swap(T heap[], int i, int j) 16swap(T heap[], int i, int j)
@@ -38,7 +39,7 @@ fixdown(T heap[], int i, int n)
38 for ( ;; ) { 39 for ( ;; ) {
39 const int l = LEFT(i); 40 const int l = LEFT(i);
40 const int r = RIGHT(i); 41 const int r = RIGHT(i);
41 int m = i; 42 int m = i;
42 43
43 if ( l < n && heap[m] < heap[l] ) 44 if ( l < n && heap[m] < heap[l] )
44 m = l; 45 m = l;
@@ -75,8 +76,8 @@ my_heapsort(T a[], int n)
75 76
76// ------------------------------------------- 77// -------------------------------------------
77 78
78struct pq { // Priority Queue 79struct pq { // Priority Queue
79 T heap[251]; 80 T heap[251];
80 int sz; 81 int sz;
81}; 82};
82 83
@@ -99,7 +100,7 @@ pq_push(struct pq *pq, T data)
99} 100}
100 101
101bool 102bool
102pq_pop(struct pq *pq, T* data) 103pq_pop(struct pq *pq, T *data)
103{ 104{
104 if ( pq->sz == 0 ) 105 if ( pq->sz == 0 )
105 return false; 106 return false;
@@ -113,8 +114,8 @@ pq_pop(struct pq *pq, T* data)
113 114
114// ------------------------------------------- 115// -------------------------------------------
115 116
116 117void
117void print_heap(T heap[], int n) 118print_heap(T heap[], int n)
118{ 119{
119 if ( n ) { 120 if ( n ) {
120 printf("%d", heap[0]); 121 printf("%d", heap[0]);
@@ -124,9 +125,10 @@ void print_heap(T heap[], int n)
124 } 125 }
125} 126}
126 127
127int main(void) 128int
129main(void)
128{ 130{
129#if 0 // Heap-Testprogramm 131#if 0 // Heap-Testprogramm
130 T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 }; 132 T heap[20] = { 18, 18, 16, 9, 7, 1, 9, 3, 7, 5 };
131 133
132 print_heap(heap, 10); 134 print_heap(heap, 10);
@@ -148,5 +150,3 @@ int main(void)
148 while ( pq_pop(pq, &data) ) 150 while ( pq_pop(pq, &data) )
149 printf("%d\n", data); 151 printf("%d\n", data);
150} 152}
151
152