Heap Sort is common in written exams.
First of all, what is heap? Heap is a kind of data struct that can be seen as a Complete Binary Tree. the object to indicate heap is an array a that have two attributes: length [a] shows the number of elements in the array and heap-size [a] is the number of heap elements in the array. it means, though we have value in a [1 .. length [a], but the element after a [heap-size [a] do not belong to the heap, So heap-size [a] <= length [A]. the root of the heap (tree) is a [1], and giving any element I we can easily caculate its patent node, left child node and right child node, whichi is:
Parent (I)
Return I/2;
Left (I)
Return 2I;
Right (I)
Return 2I + 1;
There are two kind of heap: Max-heap and Min-heap. in max-heap, for every element I have CT the root shoshould obey the rule that the node is no bigger than its parent node, which means:
A [Parent (I)]> = A [I]
And in Min-heap, on the contrary, the node I is no smaller than its parent's:
A [Parent (I)] <= A [I]
In heap-sort, we use the concept of max-heap and the Min-heap can be used in construction of priority queue.