Sequence-heap sort-Dagen (heap top)

Source: Internet
Author: User

1. Little Gan
If the root is the child's presence leave the root value of the left child less than the value if the root is the root of the child's right to exist less than the rights of their child.
2. Large Root Heap
If the root is a child the presence of the root leaves many names leaving their own child values. Children the value of the root node is greater than the value of the right child.


3. Conclusion
(1) The heap is a completely binary tree (assuming the public H-layer, then the 1~H-1 layer is full, in the H layer is missing a number of right leaves).
(2) The value of the root node of the small Gan is the minimum value, and the value of the root node of the Dagen is the maximum value.
(3) The heap is suitable for sequential storage.
4. Heap Insertion algorithm
Inserts a data element into the heap so that it remains a heap.
Algorithm Description: The node is inserted into the tail of the heap, and then the node is adjusted upward by layer until it still constitutes a heap. The adjustment method is to see whether each subtree conforms to the characteristics of large (small) Gan, and the position of leaves and roots is adjusted if it does not conform.


5. Heap deletion algorithm
When the heap deletes an element, only the root node can be deleted.
Algorithm Description: The root node is deleted after the heap tail node to fill, adjust the binary tree, so that it still become a heap.
6. Heap sequencing (Dagen, small Gan similar)
The basic idea is (Dagen):
1) the keyword sequence (r1,r2 ...) will be sorted initially. Rn) is constructed into a large top heap, this heap is the initial disordered zone, the process of construction is each non-leaf node has been adjusted, the order of adjustment from the bottom to the top layer (the adjustment process contains recursion), so adjust down this binary tree is a large Gan (or small Gan);

2) Swap the top element of the heap r[1] with the last element R[n]. The new unordered area is now available (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n];

3) because the new heap top r[1] may violate the nature of the heap, the current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (R1,R2 ...). Rn-2) and the new ordered area (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 complete.


The procedure is as follows:
1) Initialize the heap: R[1..N] is constructed as a heap;

2) Swap the top element of the current unordered area R[1] with the last record of the interval, and then adjust the new unordered area to the new heap.

So for the heap ordering, the most important two operations is to construct the initial heap and the adjustment heap, actually constructs the initial heap is actually adjusts the heap the process, only constructs the initial heap is to all non-leaf nodes to adjust.


Operation Process Diagram:

From the above process. Heap sorting is also a sort of selection, a tree-shaped selection sort.

Just choose the sort directly. In order to select the maximum record from R[1...N], it takes more than n-1 times to select the maximum record from r[1...n-2], which is n-2 times. In fact, this n-2 is very much in the previous n-1 times have been done. The tree-shape selection sort takes advantage of the tree-shaped features to preserve some of the earlier comparative results. Therefore, the comparison can be reduced. For n sequences of keyword. In the worst case, each node needs to be LOG2 (n) at a time. Therefore, the worst-case time complexity is NLOG2 (n). Heap sorting is an unstable sort and is not suitable for recording fewer sorts.


Understanding of Log2 (N): Based on the process of heap sequencing, each time the value of the Gongen node is exchanged with the value of the last leaf, it is assumed that the last leaf node is exactly the smallest number. Then this leaf node will be placed in a layer of the sub-tree finally placed in the leaf node of the seat (not the position of the front leaf node). In this case, the number of layers that the leaf node passes through is just log2 (n). However, other branches of the two-branched tree that were not exchanged, because they were once large piles. So the nature of Dagen is still unchanged, which is crucial to understanding the process. C language programs such as the following:

/* Heap Sort (Dagen) */#include <stdio.h>/* Note: This function simply adjusts the position of the swap to a large heap. The branch that is not swapped is not processed, so you cannot pass the root node of a non-Dagen binary tree so that the function will handle it as Dagen */void heap_ajust (int *a, int i, int size)/*a to the heap storage array, size is the heap sizes */{int LC       Hild = 2*i;     I's left child node ordinal int rchild = 2*i +1; I's right child node ordinal int max = i;    /* The Subscript */int temp that holds the largest number of three vertices;            if (i <= SIZE/2)//Assuming I is a leaf node it is not necessary to adjust {if (lchild<=size && A[lchild]>a[max]) {        max = Lchild;        } if (Rchild<=size && A[rchild]>a[max]) {max = Rchild;            } if (max! = i) {temp = A[i];/* Exchange a[i] and A[max] value */a[i] = A[max];a[max] = temp; Heap_ajust (A, max, size); /* The position that was swapped was once a large heap, and now it may not be a large heap so it needs to be adjusted again to make it a large heap structure */}}}void build_bheap (int *a, I    NT size)/* Build a large root heap */{int i; for (I=SIZE/2; I >= 1; i--)/* Non-leaf node the maximum ordinal value 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;/* swaps heap top and last element, that is, each time the largest of the remaining elements is placed in the last face */he Ap_ajust (A, 1, i-1);    /* Once again, the top node of the heap is resized to become a large top heap, and only the branches that are exchanged may not Dagen */}} int main (int argc, char *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);p rintf ("Sort over:");    For (i=1;i <= size; i++) printf ("%d", a[i]);    printf ("\ n"); return 0;}
Program execution is:

Kauboven Address: http://www.cnblogs.com/dolphin0520/archive/2011/10/06/2199741.html


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Sequence-heap sort-Dagen (heap top)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.