I will go to the Baidu test tomorrow. Now I want to embrace Buddhism.
Theory: Concise modern magic
InProgramIn the design field, the concept of heap mainly involves two aspects:
- A Data Structure, logically A Complete Binary Tree, is stored as an array object (Binary heap ).
- The garbage collection storage area is a memory area that can be programmed by the software system.
The heap mentioned in this Article refers to the former.
The time complexity of heap sorting is O (nlgn), which is the same as that of fast 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.
Basic knowledge
The heap is usually represented by an array, such as the length (A) of array A and the number of elements in the array heapsize (). In general, heapsize (a) <= length (a), because some elements in array a may not exist in the heap.
Assume that node I is the node marked as I in array.
- Parent (I): Return floor (I/2); // subscripts of the I parent node. Floor (I) indicates the largest integer smaller than I.
- Left (I): return 2 * I; // left subnode of I
- Right (I): return 2 * I + 1; // The right subnode of I
The height of heap a with n elements is floor (lgn ).
Basic heap operations
- Maxheapify (A, I ):
Maintain the nature of the heap. Suppose array A and subscript I. Assume that the left and right subtree with left (I) and right (I) as the root node is already the largest heap, node I value may be smaller than its subnode. Adjust the position of node I.
- Buildmaxheap ():
Creates a maximum heap from a given array. The elements in the sub-array a [floor (n/2) + 1 ...... N] Are the leaf nodes of the tree (the basic nature of the Complete Binary Tree ). From the index ceiling (n/2) to 1, execute maxheapify on each element to get a maximum heap.
- Heapsort ():
Heap sortingAlgorithmThe basic idea is to create array A as a maximum heap, and then swap the root (maximum element) of the heap and the last leaf node X, remove X from the heap to form a new heap A1, and then repeat the above actions until there is only one node in the heap.
- Priority queue algorithm-add an element value (priority): heapincreasekey (A, I, key)
After the priority of an element is increased (the value of the element), the element should move up to maintain the heap nature.
- Priority queue algorithm-insert an element: insert (S, x) to insert X into priority queue S.
The main idea is to expand a new leaf node that is infinitely small after the last leaf node of the heap, and then increase its value to the value of X.
Originally, I only wanted to write a small program, but I found that I could write a more general program. Take the minimum heap as an example to describe the process of heap building and sorting.
After the above two steps, a minimum heap is successfully created. The sorting process is to take out the top element of the heap and push it to the temporary array. Then, the top element and the last element are exchanged, and the last element is pop until there is no element in the heap. In this way, an ordered array is obtained and copied to the heap.
The complete procedure is as follows:
// Minimum heap sorting and maximum heap sorting # Include <Algorithm> # Include <Functional> # Include <Vector> # Include <Iostream> Using Namespace STD; Template <Typename type> Class Heap { Public : Heap ( Const Vector <type> & A_array) {m_array.assign (a_array.begin (), a_array.end ();} Template <Typename compare> Void Sort (compare comp ); Void Printarray ( Const Vector <type> & A_array ); Private : Vector <Type> M_array; // If comp is less <type>, the large number is sunk, and the minimum heap is created, sorted from small to large. // If comp is set to greater <type>, the decimal point is sunk and the largest heap is created, sorted from large to small. Template <typename compare> Void Creatheap (compare comp ); // Create heap Template <Typename compare> Void Downelement ( Int A_elem, compare comp ); // Sinking Element }; Template <Typename type> Template <Typename compare> Void Heap <type> : Sort (compare comp) {printarray (m_array); creatheap (COMP ); // Heap Creation Vector <type> Array; For ( Int I = m_array.size ()- 1 ; I> = 0 ; I -- ) {Array. push_back (m_array [ 0 ]); // Reserve heap top Swap (m_array [0 ], M_array [I]); // Exchange M_array.pop_back (); // Remove the last element Downelement ( 0 , Comp ); // Sink the new first element } Printarray (array); m_array.assign (array. Begin (), array. End ();} Template <Typename type> Template <Typename compare> Void Heap <type>: Creatheap (compare comp ){ // From the last non-leaf node, change each parent node to the minimum heap. For ( Int I = m_array.size ()/ 2 - 1 ; I> = 0 ; I -- ) {Downelement (I, comp) ;}} Template <Typename type> Template <Typename compare> Void Heap <type >:: downelement ( Int A_elem, compare comp)// Sinking Element { Int Min; // Set minimum element subscript Int Index = a_elem; // Element subscript currently sunk While (Index * 2 + 1 <M_array.size ()) // Left Node {Min = Index *2 + 1 ; If (Index * 2 + 2 <M_array.size ()) // Right node exists { // Compare left and right nodes and select the smallest If (COMP (m_array [Index * 2 + 2 ], M_array [Min]) {min = Index * 2 + 2 ;}} // Compare with a child node. If the parent node is the smallest, it ends. If (COMP (m_array [Index], m_array [Min]) { Break ;} Else // Select the minimum element to the parent node {Swap (m_array [Min], m_array [Index]); index = Min ;}} Template <Typename type> Void Heap <type >:: printarray ( Const Vector <type> & A_array ){ For ( Int I = 0 ; I <a_array.size (); I ++ ) {Cout <A_array [I] < " " ;} Cout < Endl ;} Int Main () {Vector < Int > Array; For ( Int I = 10 ; I < 20 ; I ++ ) {Array. push_back (I);} random_shuffle (array. Begin (), array. End ()); // Disordered order Heap <Int > Heap (array); heap. Sort (less < Int > (); Heap. Sort (greater < Int > ()); Return 0 ;}