The first thing to understand is what the heap means: either all nodes are less than their child node data, or they are not smaller than their child node data
The core idea of heap sequencing: to meet all nodes to meet above two points, how to complete, look below
Steps for heap sorting:
1. First of all to build a large pile or small pile, in the process of construction is to adjust the location of the node, first of all from the last node of Mother's Day point, according to the meaning of the heap adjustment. Why not the last one or the other? Because it is necessary to ensure completeness and necessity, just start with the last Mother's Day. (The following heap defaults to a sequential structure, starting with index 0, so some binary tree features refer to the binary tree) until the node with the index node is 0. When the adjustment is complete, it becomes a heap, but the data here is not sorted well, so the next adjustment order.
2. Start with the last data, exchange with the first data, and then adjust the first data according to the meaning of the heap. Why first select the last data? Because by default, the last one is either larger or smaller, you can meet the adjustment requirements. Then consider all current data minus the last one, because this is already the largest or smallest, no need to reconsider. Sorting is completed until the adjustment has no data.
The specific legend is no longer identified, there is this hobby can refer to other books or online introduction, the following look at the heap sorting code:
Copy Code code as follows:
int Heapsort (mergetype* L)
{
int i = 0;
if (! L->elem)
{
return-1;
}
Creating heaps
for (int i = l->len/2-1; i >= 0; i--)
{
Heapadjust (L, I, l->len-1);
}
Heap Sort
for (i = l->len-1; i >= 0; i--)
{
Swap (L->elem[i], l->elem[0]);
Heapadjust (L, 0, i-1);
}
return 0;
}
Attention:
1 because of the parent-child node relationship, the first Data index of the For loop is actually l,len-1, but its parent node (i) is associated with the current node (p): p = 2i+1 or 2i+2; If the node that stores the data is not 0 but 1, this is p=2i or p=2i+ 1, please refer to the proof of the book, so the current parent node: i = (p-1)/2 = (l.len-1-1)/2 = l.len/2-1
2) since the second adjustment of data is from the last data, so need to exchange data swap, and then the current vertex data is the first data heap adjustment, but at this time the adjusted object is only (0~i) this data, the other has been sorted, so no need to adjust
Next look at the adjustment code, as follows:
Copy Code 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;
}
This is used at one level is the data direct exchange, in fact this is not necessary, because finally put the data to the last position, so you can also use the following code to reduce the number of copies
Copy Code code as follows:
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])//elect the eldest child
{
i++;
}
if (Ntempkey >= l->elem[i])//If the current node is greater than the maximum child children exit
{
Break
}
L->elem[npos] = l->elem[i]; Otherwise the data exchange
NPOs = i;
}
L->elem[npos] = Ntempkey;
return 0;
}
Here you can reduce the number of replication operations, which is commonly known as the mobile operation times; here for the start node of the loop according to the above inference, the child node should be p=2i+1, so the first should be 2*npos+1, corresponding to the current node to compare the child, the right child for 2*npos+ 2, which is the left child +1, please see the note.
Time complexity: O (NLOGN), the analysis process is briefly