summaryrefslogtreecommitdiff
path: root/2_anno/Algoritmi/data_structures
diff options
context:
space:
mode:
Diffstat (limited to '2_anno/Algoritmi/data_structures')
-rw-r--r--2_anno/Algoritmi/data_structures/heap.c18
1 files changed, 15 insertions, 3 deletions
diff --git a/2_anno/Algoritmi/data_structures/heap.c b/2_anno/Algoritmi/data_structures/heap.c
index d0041be..6939b0e 100644
--- a/2_anno/Algoritmi/data_structures/heap.c
+++ b/2_anno/Algoritmi/data_structures/heap.c
@@ -5,7 +5,7 @@
unsigned heapsize = 0;
int compare(int x, int y) {
- return (HEAP_TYPE) ? x < y : x > y;
+ return (!HEAP_TYPE) ? x < y : x > y;
}
void swap(int* a, int x, int y) {
@@ -29,8 +29,8 @@ void heapify(int* a, int n, int i) {
int r = l | 1;
int max = i;
- if(l <= n && compare(a[l], a[max])) max = l;
- if(r <= n && compare(a[r], a[max])) max = r;
+ if(l <= n && !compare(a[l], a[max])) max = l;
+ if(r <= n && !compare(a[r], a[max])) max = r;
if(max != i) {
swap(a, i, max);
@@ -73,6 +73,18 @@ int main() {
heapsize = 6;
int* a = malloc(sizeof(int) * 7);
+ a[1] = 5;
+ a[2] = 24;
+ a[3] = 1;
+ a[4] = 12;
+ for(unsigned i = 1; i < 7; ++i)
+ printf("%d ", a[i]);
+ printf("\n");
+ build(a, 4);
+ for(unsigned i = 1; i < 7; ++i)
+ printf("%d ", a[i]);
+ printf("\n\n");
+
for(unsigned i = 1; i < 7; ++i)
a[i] = i;
build(a, 6);