First, we need to understand the meaning of heap: either all nodes are not greater than the data of their child nodes, or none is less than the data of their child nodes.
The core idea of heap sorting is to satisfy the above two points for all nodes. For details, see the following:
Heap sorting steps:
1. first, you need to build a large or small top heap. during the construction process, you actually need to adjust the node location. First, you need to adjust the Mother's Day point of the last node according to the meaning of the heap. Why is it not the last one or another? To ensure integrity and no necessity, you only need to start from the last Mother's Day (the following Heap has a sequential structure by default, starting from index 0, so for the features of some binary trees, refer to the binary tree) until the index node is 0. After the adjustment, it becomes a heap, but the data here is not sorted well, so the next part is adjusted.
2. Start from the last data, exchange with the first data, and adjust the first data according to the meaning of the heap. Why select the last data first? By default, the last one is large or small, which can meet the adjustment requirements. In this case, all the current data is subtracted from the last one, because it is already the largest or the smallest, and you do not have to consider it again .. The sorting is completed until no data is adjusted.
The legend is no longer identified. For more information about this hobby, see other books or the introduction on the Internet. See the heap sorting code below:
Int HeapSort (MergeType * L) {int I = 0; if (! L-> elem) {return-1;} // create a heap for (int I = L-> len/2-1; I> = 0; I --) {HeapAdjust (L, I, L-> len-1);} // heap sorting for (I = L-> len-1; I> = 0; I --) {swap (L-> elem [I], L-> elem [0]); HeapAdjust (L, 0, I-1);} return 0 ;}Note:
1) due to the relationship between parent and child nodes, the first data index of for loop is actually L, len-1, but the relationship between its parent node (I) and the current node (p: p = 2i + 1 or 2i + 2; if the first index of the node storing data is not 0 but 1, here p = 2i or p = 2i + 1, please refer to the proof of the relevant books, so the current parent node: I = (p-1)/2 = (L. len-1-1)/2 = L. len/2-1
2) because the last data is used to adjust the data again, swap needs to exchange the data and then perform the heap adjustment of the current vertex data, that is, the first data, however, the adjusted object is (0 ~ I) The data is sorted by other data, so no adjustment is required.
Let's take a look at the adjustment Code as follows:
int HeapAdjust(MergeType* L, int nPos, int nEnd){for (int i = nPos*2+1; i < nEnd ; i = 2*i+1){if (L->elem[i] <= L->elem[i+1]){i++;}if (L->elem[nPos] >= L->elem[i]){break;}swap(L->elem[nPos], L->elem[i]);nPos = i;}return 0;}The following code is used to directly exchange data at a level. In fact, this is not necessary because the data is placed at the last position, reduce the number of copies
Int HeapAdjustEx (MergeType * L, int nPos, int nEnd) {int nTempkey = L-> elem [nPos]; for (int I = nPos * 2 + 1; I <nEnd; I = 2 * I + 1) {if (L-> elem [I] <= L-> elem [I + 1]) // select the largest child {I ++;} if (nTempkey> = L-> elem [I]) // if the current node is greater than the maximum child, exit {break ;} l-> elem [nPos] = L-> elem [I]; // otherwise, nPos = I;} L-> elem [nPos] = nTempkey; return 0 ;}Here, we can reduce the number of replication operations, that is, the number of mobile operations.
Time Complexity: O (nlogn). The analysis process is omitted.