Heap sorting is more difficult to understand than several sorting methods. When I started to learn the data structure, I was a little confused. If I didn't figure out the principle, I checked the code. The more I got confused, the more difficult I got, in addition, at that time, the University did not realize what a good algorithm base meant. Therefore, it was not until the graduation of basic data structures and algorithms that were a mess. I had to take a look at it when I was looking for a job. Now I have been working for a year, in the face of a gloomy working environment and salary, I have a motivation to learn. If you are as hard-pressed as me, take the time to lay the foundation.
I am more concise and clear, so the Code provided is basically better understood, because I can't understand it, so I can't compare it with the code for verification or writing. To understand heap sorting, you must remember to use the full binary tree to sort array elements, instead of building a full binary tree.
Not to mention nonsense. Let's look at the definition of heap (the 10 books are basically the same. Here we are from wiki)
Heapsort is a sort algorithm designed by using the data structure of heap. A heap is a structure that is similar to a completely binary tree and meets the heap nature. That is, the key value or index of a subnode is always smaller than or greater than its parent node. There are a lot of good information on the Wiki. If you have the conditions to read English as much as possible, you can generally understand it. If you still don't understand it after reading the Wiki, You can Google, these common algorithms will always find one that you can understand.
Package COM. mars. sort; public class heapsortwiki {private void siftdown (INT [] A, int start, int end) {int root = start; // child node, int child, swap; // while root has at least one child while (root * 2 + 1 <= END) {Child = root * 2 + 1; // initially, the root node is used as the node to be switched. If the root node is compared with the Left and Right subnodes, swap = root indicates no adjustment is required, it indicates that the subtree // is a small root heap. You do not need to adjust swap = root; // check whether the root node is smaller than the left subtree if (a [swap] <A [child]) {swap = Child;} // here is a technique Coincidentally, if the left node is greater than the parent node, the comparison is // between the left and right nodes, otherwise, if (a [swap] <A [Child + 1]) {swap = Child + 1 ;} // check whether swap needs to be switched = root indicates that it is already a small heap. // If (swap! = Root) {int TMP = A [root]; A [root] = A [swap]; A [swap] = TMP ;} else {// swap = root indicates a small heap return ;}}} /*** check whether the bottom layer of the slave tree meets the heap attributes * @ Param A * @ Param count */private void heapify (INT [] A, int count) {// start int start = (count-2)/2 from the last parent node; while (Start> = 0) {// sift down the node at index start to // the proper place such that all nodes // below the start index are in heap order siftdown (A, start, count-1); Start-= 1 ;}} public void heapsort (INT [] A, int count) {heapify (A, count); int end = count-1; while (end> 0) {int TMP = A [end]; A [end] = A [0]; A [0] = TMP; end-= 1; heapify (A, end) ;}} public static void main (string [] ARGs) {int A [] = {1, 0, 4,-1, 5, 2, 6, 9, 8, 7, 3}; new heapsortwiki (). heapsort (A, 11); For (int B: a) {system. out. print (B + "");}}}
Http://www.cnblogs.com/zabery/archive/2011/07/26/2117103.html
Http://en.wikipedia.org/wiki/Heapsort