C Example: Min-Heap: sift-up insert, sift-down extract and priority order

🗓 Last updated: 2026-11-30👤 Written by: Gray✓ Reviewed by: Alex🏷 Category: Data Structures

An array-based min-heap: insertion sifts the new element up, extraction moves the last element to the root and sifts it down, keeping every parent no larger than its children. Inserts eight numbers, prints the heap after each step, then extracts them in order 1, 3, 7, 8, 19, 25, 31, 42.

🔥 Advanced Reference entries: kw-intkw-structfn-printf
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
push 42 -> heap(1): 42
push  7 -> heap(2): 7 42
push 19 -> heap(3): 7 42 19
push  3 -> heap(4): 3 7 19 42
push 25 -> heap(5): 3 7 19 42 25
push  8 -> heap(6): 3 7 8 42 25 19
push  1 -> heap(7): 1 7 3 42 25 19 8
push 31 -> heap(8): 1 7 3 31 25 19 8 42

pop order: 1 3 7 8 19 25 31 42
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏