Understanding of the Heap Sort

Top with a small stack, for example

The first step is to set up the stack to ensure that the whole tree root is the smallest, and then sub-tree for each node represents, the value of the node is the smallest sub-tree

Then a second step, a node of a root node and exchange

Why do this?

The first step, we have determined the root node is the smallest of these elements, then this node and the last node exchange, to adjust before the n - 1 elements that make up the stack.

The reason why each will be exchanged back and the top element node, because we can guarantee the establishment of the heap is top of the heap in the current range of the smallest stack, which is a time to adjust one, to adjust the size of the heap

Getting smaller and smaller, so you can achieve a sorted array

 

Guess you like

Origin www.cnblogs.com/letlifestop/p/11772160.html