In the software design field, the concept of "heap" mainly involves two aspects:
One is the data structure, logically A Complete Binary Tree, and the storage is an array object (Binary heap ).
The other is the garbage collection storage area, which is the memory area that the software system can program.
The heap mentioned in this Article refers to the former. In addition, this articleArticleThe element values in the Heap take Integer as an example.
The time complexity of heap sorting isO (nlog2n)The time complexity is the same as that of quick sorting. however, in practice, we usually adopt fast sorting instead of heap sorting. this is because a good implementation of fast sorting is often better than that of heap sorting. heap sorting is mainly used to form and process priority queues. in addition, if the calculation requirement is a class priority queue (for example, as long as the maximum or minimum elements are returned, there is only a limited insert requirement), the heap is also a suitable data structure.
Heap sorting
Heap sorting is a kind of sorting. Is an unstable sorting method. The time complexity is O (nlog2n ).
Heap sorting is characterized by the sequential storage structure of a Complete Binary Tree in the sorting process. The inherent relationship between parent and child nodes in the Complete Binary Tree is used, select the record with the maximum or minimum keyword in the unordered area.
Basic Ideas
1. Create an array to be sorted as a large root heap. The top element of the heap is the largest element in the heap.
2. Swap the top elements of a large root heap with the last element in the unordered zone, enter the last position of the unordered zone as an example, and then change the new unordered zone to a large root heap.
Repeated operations: unordered areas are decreasing, and ordered areas are increasing.
Initially, the entire array is unordered. After the first exchange, the unordered area is reduced by one, and the ordered area is increased by one.
Each swap is the top element of a large pile inserted into the ordered area, so the ordered area is kept orderly.
Big root heap and small root heap
Heap: A Complete Binary Tree.
Big root heap: a heap with smaller subnodes than itself
Small root heap: the size of the child nodes of all nodes is larger than that of the child nodes.
Relationship between heap and array
Heap is a logical structure (the storage format of the data in an image), while an array is the actual storage structure of the data (corresponding to the storage address of the data ), the relationship between the root node and the Left and Right subnodes in the storage array is as follows: assume that the position of the root node in the array (array subscript) is I, the position of the Left node in the array (array subscript) is I * 2 + 1, and the position of the right node in the array (array subscript) is I * 2 + 2.
The above are basic knowledge points. Code As follows:
// Heap sortingAlgorithm(Pass the name of the array to be sorted, that is, the address of the array. The operations on the input parameter array are reflected in the real parameter array)
Private Static Void Heapsortfunction ( Int [] Array)
{
Try
{
Buildmaxheap (array ); // Create a big top push (the initial status is as follows: Overall unordered)
For ( Int I = Array. Length - 1 ; I > 0 ; I -- )
{
Swap ( Ref Array [ 0 ], Ref Array [I]); // Switch the top element of the heap to the last element in the unordered area in sequence (enable the top element to enter the ordered area)
Maxheapify (array, 0 , I ); // Re-adjust the unordered area to a large heap
}
}
Catch (Exception ex)
{}
}
/// <Summary>
/// Create a big top push (the root node is greater than the left and right subnodes)
/// </Summary>
/// <Param name = "array"> Array to be sorted </Param>
Private Static Void Buildmaxheap ( Int [] Array)
{
Try
{
// According to the nature of the Big Top heap, we can see that the elements in the first half of the array are root nodes, and the other elements are leaf nodes.
For ( Int I = Array. Length / 2 - 1 ; I > = 0 ; I -- ) // Adjust the top push from the last root node at the bottom layer
{
Maxheapify (array, I, array. Length ); // Adjust the heap
}
}
Catch (Exception ex)
{}
}
/// <Summary>
/// Adjustment Process of Big Top push
/// </Summary>
/// <Param name = "array"> Array to be adjusted </Param>
/// <Param name = "currentindex"> Position of the element to be adjusted in the array (I .e. root node) </Param>
/// <Param name = "heapsize"> Number of all elements in the heap </Param>
Private Static Void Maxheapify ( Int [] Array, Int Currentindex, Int Heapsize)
{
Try
{
Int Left = 2 * Currentindex + 1 ; // Position of the Left subnode in the array
Int Right = 2 * Currentindex + 2 ; // Position of the right subnode in the array
Int Large = Currentindex; // Records the maximum value of the root node, left subnode, and right subnode.
If (Left < Heapsize && Array [left] > Array [large]) // Compare with left subnode
{
Large = Left;
}
If (Right < Heapsize && Array [right] > Array [large]) // Compare with the right subnode
{
Large = Right;
}
If (Currentindex ! = Large) // If currentindex! = Large indicates that large has changed (that is, the left and right subnodes are larger than the root nodes)
{
Swap ( Ref Array [currentindex], Ref Array [large]); // Swap the large people in the left and right nodes with the root node (that is, implement a local Big Top heap)
Maxheapify (array, large, heapsize ); // The large position of the above adjustment Action (for the root node location of the adjustment) and perform recursive Adjustment
}
}
Catch (Exception ex)
{}
}
/// <Summary>
/// Exchange Functions
/// </Summary>
/// <Param name = "A"> Element </Param>
/// <Param name = "B"> Element B </Param>
Private Static Void Swap ( Ref Int A, Ref Int B)
{
Int Temp = 0 ;
Temp = A;
A = B;
B = Temp;
}