Refers to a heap value for each node is greater than or equal to about less balanced binary tree structure of the node value, the stack is divided into a large top of the stack (each node is equal to a value greater than about node value) and a small top stack (each node It is smaller than or equal about node values) .
Heap sort of prerequisite is to construct a heap out of here with a large stack top as an example.
Given a large top stack array is constructed.
The main idea of the big top construction heap:
1, n pieces of data;
2, taken from the data in the data to be processed, the tail is inserted into the stack, and adjusts the stack to be larger than the top;
2.1, if the node value is larger than the adjusted value of the parent node, the two nodes exchange values, this step is repeated until the root node is adjusted ;
2.2, otherwise the node is inserted into the top of the heap big, no adjustment ;
3. Repeat step 2 until all data has been removed.
Construction heap of code to achieve:
Heap also build over, then the rest is how to sort the sort of thinking is:
1, a large stack of n elements root;
2, exchanged with a root node of the current and the last stack;
3, the remaining elements of the n-1 larger than the readjustment of the top of the stack (adjusted with reference to the large top stack configured idea ideas large stack top);
4. Repeat steps 2 and 3 until the sorting is completed.
Code:
The resulting array of reverse output is a sorted array.
Public concern No. eating flesh