Binary heap is based on complete binary tree. All the nodes of a max-heap (or min-heap) follow the property that the key of a node is larger than or equal (or less than or equal to) to the keys of it’s children nodes.
We represent $N$-element heap as array $pq$ of length $N+1$. The parent of the node in position $i$ is in position $\lfloor \frac{i}{2} \rfloor$. The children of the node in position $i$ are in positions $2i$ and $2i + 1$.
Note: STL PriorityQueue is implemented using max-heap
If a node’s key becomes larger than parent’s key we do bottom-up reheapfy (swim).
If a node’s key becomes smaller than child node’s key we do top-down reheapfy (sink).
Both of them are $O(log(n))$.
Complexity is $O(n)$ (fewer than $2n$ compares and fewer than $n$ exchanges).
Insertion. To insert new element to the heap we add new element at the end of array, increment the heap size and then swim up with that key.
Removing the maximum (minimum). We take the largest key off the top, put the item from the end at the top, decrement heap size and then sink down with the element.
Both of them are $O(log(n))$.
Complexity is $O(n log(n))$. Heapsort has poor cache performance.