Min Heap in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Smallest at the Root

In a min heap each parent is less than or equal to its children, so the minimum is always h[0]. Push appends at the end and sifts up while smaller than the parent. Pop takes the root, moves the last element to the root, and sifts down by swapping with the smaller child.

Remember
push: h[n++] = v; while (h[parent] > h[i]) swap, i = parent;
pop : top = h[0]; h[0] = h[--n]; sift down to smaller child;

Cheat Sheet

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

TopicDetail
Peek minO(1)
PushO(log n)
Pop minO(log n)
Sift downSwap with the smaller child

C Examples

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

Example 1 — Push five values and pop them in order

Pops come out sorted ascending.

C
#include <stdio.h>

static void swap(int *a, int *b) { int t = *a; *a = *b; *b = t; }

static void push(int h[], int *n, int v) {
    int i = (*n)++;
    h[i] = v;
    while (i > 0 && h[(i - 1) / 2] > h[i]) {
        swap(&h[(i - 1) / 2], &h[i]);
        i = (i - 1) / 2;
    }
}

static int pop(int h[], int *n) {
    int top = h[0];
    h[0] = h[--(*n)];
    int i = 0;
    for (;;) {
        int l = 2 * i + 1, r = l + 1, s = i;
        if (l < *n && h[l] < h[s]) s = l;
        if (r < *n && h[r] < h[s]) s = r;
        if (s == i) break;
        swap(&h[i], &h[s]);
        i = s;
    }
    return top;
}

int main(void) {
    int h[16], n = 0;
    int in[] = {5, 3, 8, 1, 4};

    for (int i = 0; i < 5; i++) push(h, &n, in[i]);

    printf("heap array:");
    for (int i = 0; i < n; i++) printf(" %d", h[i]);
    printf("\nextracted:");
    while (n > 0) printf(" %d", pop(h, &n));
    printf("\n");
    return 0;
}

How It Works

  1. Pushing 1 sifts it up to the root through 5 and 3.
  2. The array is a valid heap but not sorted.
  3. Repeated pops return the smallest remaining value each time.

Edge Cases & Pitfalls

Check these before relying on Min Heap in C in real programs.

empty

Pop on empty heap

Check n > 0 before popping.

child

Swapping with the wrong child

In a min heap swap with the smaller child.

Key Takeaways

  • Root is the minimum.
  • Sift up on push.
  • Sift down on pop.

One line: In a min heap each parent is less than or equal to its children, so the minimum is always h[0].

Frequently Asked Questions

A heap where every parent is less than or equal to its children, so the smallest element is at the root.
Peek O(1); push and pop O(log n).
Continue with "Max Heap in C" next. Implement a max heap by flipping the comparisons of a min heap. The C Heaps hub lists every topic in order.

Next: Max Heap in C

Implement a max heap by flipping the comparisons of a min heap.

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