Heapify in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Fixing One Node

Heapify (sift-down) restores the heap property at index i, assuming both child subtrees are already heaps. It swaps i with its larger child (max heap) and repeats on that child until the node is in place. It takes O(log n) and is the building block of both extraction and heap construction.

Remember
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;
        swap(&a[i], &a[s]);
        i = s;
    }
}

Cheat Sheet

Keep these facts handy while you work with Heapify in C in C.

TopicDetail
PreconditionChild subtrees are heaps
ActionSift node i down
TimeO(log n)
Used byDelete and build heap

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Building a Heap in C.

Example 1 — Heapify the root

Only the root, 3, violates the max-heap property.

C
#include <stdio.h>

static void show(const char *label, const int a[], int n) {
    printf("%s:", label);
    for (int i = 0; i < n; i++) printf(" %d", a[i]);
    printf("\n");
}

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[] = {3, 9, 8, 7, 6, 5, 4};
    show("before", a, 7);
    heapify(a, 7, 0);
    show("after heapify(0)", a, 7);
    return 0;
}

How It Works

  1. 3 swaps with its larger child 9.
  2. At index 1 its children are 7 and 6, so it swaps with 7.
  3. At index 3 there are no children; the heap is restored.

Edge Cases & Pitfalls

Check these before relying on Heapify in C in real programs.

pre

Violated precondition

If subtrees are not heaps, one pass will not fix the whole array.

bound

Child bounds

Always test l < n and r < n.

Key Takeaways

  • Local repair.
  • Needs valid subtrees.
  • O(log n).

One line: Heapify (sift-down) restores the heap property at index i, assuming both child subtrees are already heaps.

Frequently Asked Questions

It moves a node down the tree until the subtree rooted at it satisfies the heap property.
Yes, heapify usually refers to the sift-down procedure.
Continue with "Building a Heap in C" next. Turn an arbitrary array into a heap in O(n) by heapifying from the last parent. The C Heaps hub lists every topic in order.

Next: Building a Heap in C

Turn an arbitrary array into a heap in O(n) by heapifying from the last parent.

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.

9 people found this page helpful