Max Heap in C

Intermediate
6 min read
Updated: Nov 2026
1 example

Largest at the Root

A max heap keeps each parent greater than or equal to its children, so the maximum is h[0]. The code mirrors a min heap with the comparisons reversed. Max heaps are the basis of heap sort and are convenient for "top element" queries.

Remember
/* sift up  : while (h[parent] < h[i])  swap
   sift down: choose the larger child */

Cheat Sheet

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

TopicDetail
Peek maxO(1)
PushO(log n)
Pop maxO(log n)
Sift downSwap with the larger child

C Examples

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

Example 1 — Max heap from five values

Pops come out sorted descending.

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 8 sifts it above 5; pushing 4 sifts it above 3.
  2. The root 8 is the maximum.
  3. Pops return values in descending order.

Edge Cases & Pitfalls

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

cmp

Wrong comparison direction

Copying min-heap code without flipping < and > builds the wrong heap.

size

Passing n by value

Push and pop must change the size, so pass a pointer.

Key Takeaways

  • Root is the maximum.
  • Mirror of min heap.
  • Basis of heap sort.

One line: A max heap keeps each parent greater than or equal to its children, so the maximum is h[0].

Frequently Asked Questions

A heap where every parent is greater than or equal to its children, so the largest element is at the root.
Flip the comparison operators in the sift-up and sift-down logic, or negate the values.
Continue with "Heap Insertion in C" next. Add at the end, then swap upward until the heap property holds. The C Heaps hub lists every topic in order.

Next: Heap Insertion in C

Add at the end, then swap upward until the heap property holds.

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.

11 people found this page helpful