Building a Heap in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Bottom-Up Construction

Instead of inserting n elements (O(n log n)), build a heap in place: call heapify for each index from the last parent n/2 - 1 down to 0. Leaves are already heaps, and each heapify assumes its children are fixed. Total work is O(n).

Remember
for (int i = n / 2 - 1; i >= 0; i--)
    heapify(a, n, i);

Cheat Sheet

Keep these facts handy while you work with Building a Heap in C in C.

TopicDetail
Start atn / 2 - 1
DirectionDown to index 0
TotalO(n)
LeavesAlready valid heaps

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Hash Tables.

Example 1 — Build a max heap in place

Input: 4, 10, 3, 5, 1.

C
#include <stdio.h>

static void heapify(int a[], int n, int i) {
    for (;;) {
        int l = 2 * i + 1, r = l + 1, s = i;
        if (l < n && a[l] > a[s]) s = l;
        if (r < n && a[r] > a[s]) s = r;
        if (s == i) return;
        int t = a[i]; a[i] = a[s]; a[s] = t;
        i = s;
    }
}

int main(void) {
    int a[] = {4, 10, 3, 5, 1};
    int n = 5;

    printf("input:");
    for (int i = 0; i < n; i++) printf(" %d", a[i]);

    for (int i = n / 2 - 1; i >= 0; i--) heapify(a, n, i);

    printf("\nmax-heap:");
    for (int i = 0; i < n; i++) printf(" %d", a[i]);
    printf("\n");
    return 0;
}

How It Works

  1. The last parent is index 1 (10); it is already larger than 5 and 1.
  2. At index 0, 4 swaps with 10, then with 5.
  3. The final array is a valid max heap.

Edge Cases & Pitfalls

Check these before relying on Building a Heap in C in real programs.

dir

Going forward instead of backward

Heapifying from 0 upward does not work; children must be fixed first.

count

Wrong start index

Start at n / 2 - 1, not n / 2.

Key Takeaways

  • Bottom-up is O(n).
  • Start at last parent.
  • Used by heap sort.

One line: Instead of inserting n elements (O(n log n)), build a heap in place: call heapify for each index from the last parent n/2 - 1 down to 0.

Frequently Asked Questions

Most nodes are near the bottom and sift down very few levels, so the total work sums to a linear bound.
Repeated insertion is O(n log n); bottom-up heapify is O(n).
Continue with "C Hash Tables" next. Build hash tables in C: hash functions, chaining, open addressing, deletion, and rehashing. The C Heaps hub lists every topic in order.

Next: Hash Tables

Build hash tables in C: hash functions, chaining, open addressing, deletion, and rehashing.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful