C Example: Min-Heap: sift-up insert, sift-down extract and priority order
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.
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
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