Min-heap insertion · Compiler

Min-heap insertion

Insert one integer into a binary min-heap stored in level order from left to right. Append it after the last entry. While it is smaller than its parent, swap the two. Using zero-based indices, the parent of position i > 0 is floor((i - 1) / 2).

Input
Line 1 contains the current heap size n (0 to 100) and the integer to insert. The remaining n integers give a valid min-heap in level order. All values are from -1000 to 1000.
Output
Print the n + 1 heap values in final level order on one line, separated by spaces.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…