Remove the minimum value, move the last item to the root, and sift downward.

Algorithm

Steps

  1. Store the heap in an array.
  2. Compare parent and child indexes instead of building explicit tree nodes.
  3. Swap only when the heap order is violated.
  4. Print the deterministic final heap state for replay comparison.

Complexity

  • Time: O(log n)
  • Space: O(1) extra
sift down After removing the root, the last value moves to the root and swaps with the smaller child until order is restored.

Visual walkthrough

C DSA Implementation

basic.c
#include <stdio.h>
void print_list(int* values, int n) {
    printf("[");
    for (int i = 0; i < n; i++) { if (i) printf(", "); printf("%d", values[i]); }
    printf("]");
}
void swap(int* a, int* b) { int t = *a; *a = *b; *b = t; }
void heap_insert(int* heap, int* n, int value) {
    heap[*n] = value; int child = *n; *n = *n + 1;
    while (child > 0) {
        int parent = (child - 1) / 2;
        if (heap[parent] <= heap[child]) break;
        swap(&heap[parent], &heap[child]);
        child = parent;
    }
}
int heap_pop(int* heap, int* n) {
    int smallest = heap[0]; heap[0] = heap[*n - 1]; *n = *n - 1;
    int parent = 0;
    while (1) {
        int left = parent * 2 + 1, right = left + 1;
        if (left >= *n) break;
        int child = left;
        if (right < *n && heap[right] < heap[left]) child = right;
        if (heap[parent] <= heap[child]) break;
        swap(&heap[parent], &heap[child]);
        parent = child;
    }
    return smallest;
}
int main(void) { int heap[16] = {1, 4, 2, 9, 6, 7}; int n = 6; int popped = heap_pop(heap, &n); printf("%d -> ", popped); print_list(heap, n); printf("\n"); }

After popping the minimum, the last value moves to the root and sifts down by swapping with the smaller child.

Step 1 - Replace root

The saved minimum is 1; the last value 7 moves to the root before sifting down.

Replacement state [7, 4, 2, 9, 6] with removed value 1.1removed7root4left2smaller9i36i4

Step 2 - Swap with the smaller child

7 swaps with 2, producing the final heap [2, 4, 7, 9, 6].

Final heap after pop and one sift-down swap.2root4i17i29i36i4

Output

1 -> [2, 4, 7, 9, 6]

Implementation notes

  • C stores the heap in stack array int heap[16] = {1, 4, 2, 9, 6, 7} with scalar size int n = 6; heap_pop(heap, &n) mutates both the array and the caller's size.
  • heap_pop(int* heap, int* n) copies heap[0] into local smallest, replaces the root with heap[*n - 1], then shrinks the active heap with *n = *n - 1.
  • Sift-down uses signed int indexes: left = parent * 2 + 1 and right = left + 1. The loop stops when left >= *n or when the parent is already <= the selected child.
  • The right child is chosen only when right < *n && heap[right] < heap[left]; swaps call swap(&heap[parent], &heap[child]), which exchanges two slots through pointers using stack temporary int t.
  • The trace records [1, 4, 2, 9, 6, 7], removes 1 and moves 7 to root for [7, 4, 2, 9, 6], then swaps with smaller child 2 to [2, 4, 7, 9, 6].
  • printf("%d -> ", popped) prints the returned minimum before print_list(int* values, int n) formats the active heap through pointer-decayed array access and comma-separated printf calls.