Files
dlos/c/algorithms/sort_heap.c

22 lines
641 B
C

#include "../data_structures/heap.h"
#include "sort_heap.h"
void dlos_heapsort_array(dlos_ComparableNode *arr, int size, int (*compare)()) {
dlos_Heap heap = { .size = size, .arr = arr, .arr_size = size, .compare = compare };
dlos_max_heap(&heap);
for (int i = heap.size - 1; i > 0; i--) {
dlos_node_swap(heap.arr, 0, i);
heap.size -= 1;
dlos_sift_down(0, &heap);
}
}
// already have the heap, want to sort its array
void dlos_heapsort_heap(dlos_Heap *h) {
for (int i = h->size - 1; i > 0; i--) {
dlos_node_swap(h->arr, 0, i);
h->size -= 1;
dlos_sift_down(0, h);
}
}