From 3fcd09e7c0a634b190d07a668434775ec3b3f2a6 Mon Sep 17 00:00:00 2001 From: david Date: Mon, 6 Oct 2025 18:30:32 -0500 Subject: [PATCH] Add minor adjustments/clean-up --- c/algorithms/sort_heap.c | 1 - c/data_structures/heap.c | 12 ++++++------ c/data_structures/heap.h | 8 +++++++- c/test.c | 7 ++++++- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/c/algorithms/sort_heap.c b/c/algorithms/sort_heap.c index 51acaf3..d745907 100644 --- a/c/algorithms/sort_heap.c +++ b/c/algorithms/sort_heap.c @@ -8,5 +8,4 @@ void dlos_sort_heap(dlos_Heap *h) { h->size -= 1; dlos_sift_down(0, h); } - h->size = original_size; } diff --git a/c/data_structures/heap.c b/c/data_structures/heap.c index 388133d..e7d0f5f 100644 --- a/c/data_structures/heap.c +++ b/c/data_structures/heap.c @@ -21,12 +21,6 @@ static int right_sibling(int i) { return (i % 2 == 1) ? i + 1 : -1; } -void dlos_node_swap(dlos_ComparableNode *arr, int a, int b) { - dlos_ComparableNode temp = arr[a]; - arr[a] = arr[b]; - arr[b] = temp; -} - static int index_of_larger_child(int i, dlos_Heap *h) { int larger = left_child(i); if (larger >= h->size) { @@ -38,6 +32,12 @@ static int index_of_larger_child(int i, dlos_Heap *h) { return larger; } +void dlos_node_swap(dlos_ComparableNode *arr, int a, int b) { + dlos_ComparableNode temp = arr[a]; + arr[a] = arr[b]; + arr[b] = temp; +} + void dlos_sift_down(int i, dlos_Heap *h) { int target = index_of_larger_child(i, h); while (target > 0 && h->compare(&h->arr[i], &h->arr[target]) > 0) { diff --git a/c/data_structures/heap.h b/c/data_structures/heap.h index 87dae53..0e5d1a3 100644 --- a/c/data_structures/heap.h +++ b/c/data_structures/heap.h @@ -7,6 +7,7 @@ typedef struct dlos_ComparableNode { typedef struct dlos_Heap { int size; dlos_ComparableNode *arr; + int arr_size; int (*compare)(dlos_ComparableNode *a, dlos_ComparableNode *b); } dlos_Heap; @@ -36,7 +37,12 @@ void dlos_max_heap(dlos_Heap *h); // for (int i = 0; i < test_size; i++) { // a[i].value = &data_storage[i]; // } -// dlos_Heap heap = { .size = test_size, .arr = a, .compare = compare_nodes }; +// dlos_Heap heap = { +// .size = test_size, +// .arr = a, +// .arr_size = test_size, +// .compare = compare_nodes +// }; // dlos_max_heap(&heap); // dlos_sort_heap(&heap); // for (int i = 0; i < test_size; i++) { diff --git a/c/test.c b/c/test.c index e3ef258..a95a14f 100644 --- a/c/test.c +++ b/c/test.c @@ -22,7 +22,12 @@ int main(int argc, char *argv[]) { for (int i = 0; i < test_size; i++) { a[i].value = &data_storage[i]; } - dlos_Heap heap = { .size = test_size, .arr = a, .compare = compare_nodes }; + dlos_Heap heap = { + .size = test_size, + .arr = a, + .arr_size = test_size, + .compare = compare_nodes + }; dlos_max_heap(&heap); dlos_sort_heap(&heap); for (int i = 0; i < test_size; i++) {