heap
http://www.youtube.com/watch?v=-6-xKgLOZPM
binary heap : it is a complete binary tree.
index from 0:
i left = 2i +1; i right = 2i +2, i parent = (i-1)/2.
add. 添加到尾部. 和父亲对比. 大于就swap 父亲 自己。当最后是root的时候. root. left. right 对比.选择一个最大的swap root.
delte. swap 尾部和delete 然后调整. delete 尾部 直接删除.
build a heap:
1. iterated insertion
n total insertions, each worst than O(lgn).
worst case is O(nlgn).
2. linear time build-heap:
父比较left.如果小于.要 选父 left right最大的放到root.
binary heap sort.
O(nlgn)