diff options
author | Santo Cariotti <dcariotti24@gmail.com> | 2020-10-18 18:56:43 +0200 |
---|---|---|
committer | Santo Cariotti <dcariotti24@gmail.com> | 2020-10-20 09:08:52 +0200 |
commit | f279107065146a4940f5e73602a1c3c09e58b31d (patch) | |
tree | fd892749637a8b6c5c31ccb80bba04ade76ab87a /2_anno/Algoritmi/data_structures | |
parent | 4e063e32250312c38d5646840719b62429362b21 (diff) |
chore: name of first year folder
Diffstat (limited to '2_anno/Algoritmi/data_structures')
-rw-r--r-- | 2_anno/Algoritmi/data_structures/heap.c | 19 |
1 files changed, 15 insertions, 4 deletions
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); |