A heap is a complete binary tree stored in an array that keeps its largest element (max heap) or smallest element (min heap) at the root. It gives you O(log n) insert and extract and O(1) access to the top item, which makes it the standard structure for priority queues and heap sort. C has no built-in heap, so you implement it yourself on a plain array using index arithmetic.
For a node at index i, the parent is at (i - 1) / 2 and the children are at 2 * i + 1 and 2 * i + 2. Because the tree is complete, no pointers are needed.
The heap property compares each node with its children:
A heap is not fully sorted: siblings have no defined order, and only the parent-child relationship is guaranteed. This weaker rule is what makes insertion and removal cheap.
| Operation | How it works | Time |
|---|---|---|
| Peek | Read data[0] | O(1) |
| Push | Append at the end, then sift up | O(log n) |
| Pop | Move the last element to the root, then sift down | O(log n) |
| Build heap | Sift down from the last parent to the root | O(n) |
| Heap sort | Build a max heap, then repeatedly swap the root to the end | O(n log n) |
This implementation stores the array, its size and its capacity together, grows with realloc and reports failure instead of overflowing the buffer. It also includes a bottom-up build and an in-place heap sort.
Output:
heap_push places the new value in the first free slot and sift_up swaps it with its parent while it is larger. It stops at the root or when the parent is already larger, so it walks at most the height of the tree, O(log n).
heap_pop saves the root, moves the last element into slot 0 and calls sift_down. That function picks the larger child and swaps while a child is greater than the current node. It is written as a loop, which avoids the recursion depth of a recursive heapify.
build_max_heap starts at the last parent, index n / 2 - 1, and sifts each node down going toward the root. This is O(n), faster than pushing n values one at a time (O(n log n)). The loop for (size_t i = n / 2; i-- > 0;) avoids the classic bug of decrementing an unsigned index below zero.
Change the two comparisons in sift_up and sift_down from "greater than" to "less than", and rename the type. Everything else, including the index formulas, stays identical. For heaps of other types, such as tasks with priorities, store a struct and compare one field, or pass a comparison function pointer as qsort does.
arr[n++] = key with no capacity check is a buffer overflow; always check capacity first.-1 from an empty pop as an error code. It is indistinguishable from a real value, so return bool and use an output parameter instead.data[0] is guaranteed to be the extreme value.A binary search tree orders all elements left to right and supports sorted traversal and search. A heap only orders parents against children, so it finds the extreme value fast but cannot search efficiently.
No. The heap data structure is an ordered tree in an array. The heap memory used by malloc is unrelated, although this implementation allocates its array there.
No. C offers qsort for sorting but no priority queue. In C++ you would use std::priority_queue or std::make_heap.
O(n) and not O(n log n)?Most nodes are near the bottom and sift down only a few levels, so the total work sums to a linear bound.
A heap in C is an array where each parent is at least as large (max heap) or as small (min heap) as its children. Use index formulas for navigation, sift up after inserting, sift down after removing, build in O(n) bottom-up, and always check capacity. The same code gives you a priority queue and an in-place heap sort.