Sort - Heap Sort - Big Root Heap (Big Top Heap)
1. Small root heap
If the root node has a left child, the value of the root node is less than the value of the left child; if the root node has a right child, the value of the root node is less than the value of the right child.
2.
If the root node has a left child, the value of the root node is greater than the value of the left child; if the root node has a right child, the value of the root node is greater than the value of the right child.
3. Conclusion
(1) The heap is a complete binary tree (if there is a public h layer, then the layers 1~h-1 are full, and several right leaves are missing continuously in the h layer).
(2) The value of the root node of the small root heap is the minimum value, and the value of the root node of the large root heap is the maximum value.
(3) The heap is suitable for sequential storage.
4. The insertion algorithm of the heap inserts
a data element into the heap, making it still a heap.
Algorithm description: First insert the node into the tail of the heap, and then adjust the node up layer by layer until it still forms a heap. The adjustment method is to see whether each subtree conforms to the characteristics of the large (small) root heap. Then adjust the position of the leaves and roots.
5. Heap Deletion Algorithm When the
heap deletes elements, only the root node can be deleted.
Algorithm description: After deleting the root node, fill it with the tail node of the heap, and adjust the binary tree to make it still a heap. 6. The basic idea of
heap sorting (large root heap, small root heap is similar) is (big root heap): 1) The initial to-be-sorted key sequence (R1, R2....Rn) is constructed into a large top heap, and this heap is In the initial unordered area, the construction process is that each non-leaf node is adjusted once, and the adjustment order is from the bottom layer to the top layer (recursion is included in the adjustment process), so that the binary tree after adjustment is a large root heap (or small root heap as a whole). Root heap); 2) Swap the top element R[1] of the heap with the last element R[n], and get a new disordered area (R1, R2,...Rn-1) and a new The ordered region (Rn) of , and satisfy R[1,2...n-1]<=R[n];
3) Since the new heap R[1] after the swap may violate the properties of the heap, it is necessary to adjust the current disordered area (R1, R2,...Rn-1) to a new heap, and then change R again [1] Exchange with the last element of the disordered region to obtain a new disordered region (R1, R2....Rn-2) and a new ordered region (Rn-1, Rn). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is completed.
The operation process is as follows:
1) Initialize the heap: construct R[1..n] as a heap;
2) Swap the heap top element R[1] of the current unordered area with the last record of the area, and then replace the new unordered area with the last record in the area. The sequence area is adjusted to the new heap.
Therefore, for heap sorting, the two most important operations are to construct the initial heap and to adjust the heap. In fact, the construction of the initial heap is actually the process of adjusting the heap, but the construction of the initial heap is to adjust all non-leaf nodes.
Understanding of log2(n): According to the process of heap sorting, each time the value of the root node of the big root heap is exchanged with the value of the last leaf, then if the last leaf node happens to be the smallest number, then this leaf node It will be placed layer by layer in the subtree and finally placed in the position of the leaf node (not the position of the previous leaf node), so that the number of layers this leaf node passes through is exactly log2(n). However, other branches of the binary tree without exchange, because they used to be big root heaps, the properties of big root heaps have not changed, which is very important to understand the program.
The C language program is as follows:
#include <stdio.h> /*Note: This function will only adjust the swapped position to the big root heap, the unswapped branch will not be processed, So you can't pass the root node of a non-big root heap binary tree and let this function process it as a big root heap */ void heap_ajust(int *a, int i, int size) /*a is the heap storage array, size is the size of the heap*/ { int lchild = 2*i; //i's left child node number int rchild = 2*i +1; //i's right child node number int max = i; /*Store the subscript of the largest number among the three vertices*/ int temp; if(i <= size/2) //If i is a leaf node, no adjustment is required { if(lchild<=size && a[lchild]>a[max]) { max = lchild; } if(rchild<=size && a[rchild]>a[max]) { max = rchild; } if(max != i) { temp = a[i]; /*Swap the values of a[i] and a[max]*/ a[i] = a[max]; a[max] = temp; heap_ajust(a, max, size); /*The swapped position used to be the big root heap, but now it may not be the big root heap So it needs to be re-adjusted to make it a big root heap structure */ } } } void build_bheap(int *a, int size) /*Build a big root heap*/ { int i; for(i=size/2; i >= 1; i--) /*The maximum sequence number of non-leaf nodes is size/2*/ { heap_ajust(a, i, size); /*Every non-leaf node needs to call this function*/ } } void heap_sort(int *a, int size) /* heap sort*/ { int i; int temp; build_bheap(a, size); for(i=size; i >= 1; i--) { temp = a[1]; a[1] = a[i]; a[i] = temp; /*Swap the top and last elements of the heap, that is, put the largest of the remaining elements at the end each time*/ heap_ajust(a, 1, i-1); /*Re-adjust the top node of the heap to become the big top heap, only the swapped branch may not be the big root heap*/ } } int main(int argc, charchar *argv[]) { int a[]={0,16,20,3,11,17,8}; int size = sizeof(a)/sizeof(int) -1; int i; printf("size = %d\n", size); heap_sort(a, size); printf("Sort over:"); //Sort over for(i=1;i <= size; i++) printf("%d ", a[i]); printf("\n"); return 0; }
int size = sizeof(a)/sizeof(int) -1; ①
Calculate the total length of the Int array a in bytes, sizeof(int) indicates the length of the current system int
1. It is 2 under the 16-bit int platform;
2. It is 4 under the 32-bit int platform;
3. It is 8 under the 64-bit int platform.
The statement at ① will calculate the total number of bytes in the int a[] array
Output result:
Original: https://blog.csdn.net/laoniu_c/article/details/38513601