From f279107065146a4940f5e73602a1c3c09e58b31d Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Sun, 18 Oct 2020 18:56:43 +0200 Subject: chore: name of first year folder --- 2_anno/Algoritmi/data_structures/heap.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to '2_anno/Algoritmi/data_structures') diff --git a/2_anno/Algoritmi/data_structures/heap.c b/2_anno/Algoritmi/data_structures/heap.c index 56c3d3d..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); @@ -56,7 +56,6 @@ int* heapsort(int* a, int n) { build(a, n); for(unsigned i = 0; i < n; ++i) { b[i] = extract(a); -; } return b; @@ -74,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); -- cgit v1.2.3-18-g5258