summaryrefslogtreecommitdiff
path: root/Year_2/Algorithms/data_structures/heap.c
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2023-01-07 18:47:11 +0100
committerSanto Cariotti <santo@dcariotti.me>2023-01-07 18:47:11 +0100
commitba36beaec6d37d26b075d96e58aad73151d6d39e (patch)
tree23226d5a3d5b49616abbb37e1ba3f9666faa489d /Year_2/Algorithms/data_structures/heap.c
parenta5534f16ecaf4b93f1bcd775f1e1df64203b3b65 (diff)
Fix algs structure
Diffstat (limited to 'Year_2/Algorithms/data_structures/heap.c')
-rw-r--r--Year_2/Algorithms/data_structures/heap.c95
1 files changed, 0 insertions, 95 deletions
diff --git a/Year_2/Algorithms/data_structures/heap.c b/Year_2/Algorithms/data_structures/heap.c
deleted file mode 100644
index be7024b..0000000
--- a/Year_2/Algorithms/data_structures/heap.c
+++ /dev/null
@@ -1,95 +0,0 @@
-#include<stdio.h>
-#include<stdlib.h>
-#define HEAP_TYPE 0 // if 0 is max heap, else min heap
-
-unsigned heapsize = 0;
-
-int compare(int x, int y) {
- return (!HEAP_TYPE) ? x < y : x > y;
-}
-
-void swap(int* a, int x, int y) {
- int temp = a[x];
- a[x] = a[y];
- a[y] = temp;
-}
-
-void insert(int* a, int k) {
- a[++heapsize] = k;
- unsigned i = heapsize;
-
- while(i > 1 && compare(a[i>>1], a[i])) {
- swap(a, i, i >> 1);
- i >>= 1;
- }
-}
-
-void heapify(int* a, int n, int i) {
- int l = i << 1;
- 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(max != i) {
- swap(a, i, max);
- heapify(a, n, max);
- }
-}
-
-void build(int* a, int n) {
- for(unsigned i = n/2; i > 0; --i) {
- heapify(a, n, i);
- }
-}
-
-int extract(int* a) {
- swap(a, 1, heapsize);
- --heapsize;
- heapify(a, heapsize, 1);
- return *(a+heapsize+1);
-}
-
-int* heapsort(int* a, int n) {
- int* b = malloc(sizeof(int) * n);
- build(a, n);
- for(unsigned i = n-1; i != -1; --i) {
- b[i] = extract(a);
- }
-
- return b;
-}
-
-void increase_key(int* a, int i, int k) {
- swap(a, i, k);
- while(i > 1 && compare(a[i>>1], a[i])) {
- swap(a, i, i >> 1);
- i >>= 1;
- }
-}
-
-int main() {
- heapsize = 0;
- int* a = malloc(sizeof(int) * 7);
-
- insert(a, 5);
- insert(a, 24);
- insert(a, 1);
- insert(a, 12);
- insert(a, 35);
- insert(a, -1);
-
- printf("heap = ");
- for(unsigned i = 1; i <= heapsize; ++i)
- printf("%d ", *(a+i));
-
- int n = heapsize;
- int* b = heapsort(a, heapsize);
- printf("\nheapsort = ");
- for(unsigned i = 0; i < n; ++i)
- printf("%d ", *(b+i));
- free(b);
- free(a);
- return 0;
-}