Seven-heap and heap ordering of vernacular classical algorithm series

Source: Internet
Author: User

Heap sorting and high-speed sorting , merge sort are the same as the time complexity of O (N*LOGN) several common sorting methods. Before learning heap sequencing, we first explain what is a two-fork heap in data structures. Definition of binary heap

A binary heap is a completely binary tree or an approximate total binary tree.

The binary stack satisfies two characteristics:

1. The key value of the parent node is always greater than or equal to (less than or equal to) the key value of whatever child node.

2. Each node of the Saozi right subtree is a binary heap (both the largest heap or the smallest heap).

The maximum heap is when the key value of the parent node is always greater than or equal to whatever the key value of a child node. The minimum heap when the key value of the parent node is always less than or equal to whatever the key value of a child node. Show a minimum heap:

Since several other heaps (two-item heap, Fibonacci Ponachi, etc.) are used less often, the two-fork heap is referred to as a heap.

Storage of Heaps

The heap is usually represented by an array, and the parent node of the I node is labeled (i–1)/2. The index of the left and right sub-nodes is 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node of the left and right sub-nodes subscript 1 and 2 respectively.

Heap operation--Insert Delete

The following gives the "data structure C + + language description" of the minimum heap of the establishment of the insertion and deletion of the plot, and then give my implementation code, it is best to look at the explicit diagram before going to see the code.

The insertion of a heap

Each insertion is a new data placed at the end of the array. The ability to discover from the parent node of this new data to the root node must be an ordered sequence, and now the task is to insert this new data into this ordered data--which is similar to inserting a data into an ordered interval in a direct insert sort , comparing the second of the Vernacular classic algorithm series Three implementations of direct insertion sorting it's not hard to write the adjustment code for the heap when inserting a new data:

  The new Tim I node  has its parent node (i-1)/2void minheapfixup (int a[], int i) {    int J, temp;temp = A[i];j = (i-1)/2;      Parent node while (J >= 0 && I! = 0) {if (A[j] <= temp) break;a[i] = a[j];     Move the larger sub-node down, replacing its sub-node i = J;j = (i-1)/2;} A[i] = temp;}

A shorter expression is:

void Minheapfixup (int a[], int i) {for (int j = (i-1)/2; (J >= 0 && I! = 0) && a[i] > A[j]; i = j, j = (i-1)/2) Swap (A[i], a[j]);}

When inserting:

Add new data to the minimum heap nnumvoid minheapaddnumber (int a[], int n, int nnum) {A[n] = Nnum; Minheapfixup (A, n);}
Removal of heaps

By definition, only the No. 0 data can be deleted at a time in the heap. To facilitate rebuilding the heap, the actual operation is to assign the value of the last data to the root node and then start from the root node with a top-down adjustment. Adjust the first in the left and right son node to find the smallest, assuming that the parent node is smaller than the smallest sub-node is not necessary to adjust, but the parent node and it after the exchange and then consider the following node. Equivalent to the "sinking" process of a data from the root node. The following code is given:

  starting from the I-node adjustment, n is the total number of nodes starting from 0 compute the child nodes of the I node are 2*i+1, 2*i+2void minheapfixdown (int a[], int i, int n) {    int J, temp;temp = A[i] ; j = 2 * i + 1;while (J < N) {if (j + 1 < n && a[j + 1] < A[J])//Find the smallest j++;if (a[j] >= temp) break in the left and right child; A[i] = a[j];     Move the smaller sub-node upward, replacing its parent node i = j;j = 2 * i + 1;} A[i] = temp;} Delete the number of void Minheapdeletenumber (int a[], int n) {Swap (a[0], a[n-1]) in the minimum heap; Minheapfixdown (A, 0, n-1);}
Stacking arrays

After the heap has been inserted and deleted, consider how to heap a data. You need to get the data out of the array, one by one, to build the heap, no! Look at an array first, for example:

It is obvious to the leaf node that it is already a legitimate heap, namely 20,60, 65, 4, and 49 are each a legitimate heap. It is only possible to adjust downward from the beginning of the a[4]=50. Then take a[3]=30,a[2] = 17,a[1] = 12,a[0] = 9 to make a downward adjustment operation is possible. These steps are shown:

Write the code for the stacked array:

Establish minimum heap void makeminheap (int a[], int n) {for (int i = N/2-1; I >= 0; i--) Minheapfixdown (A, I, n);}


At this point, the heap operation is all finished (note 1), and then see how to use the heap of this data structure to sort.

Heap Sort

The first thing you can see is that the No. 0 data in the heap after the heap is built is the smallest data in the heap. Remove this data and run the next heap delete operation. The No. 0 data in the heap is the smallest data in the heap, repeating the above steps until only one of the data in the heap is taken out directly.

Because the heap is also modeled with an array, the first time the array is stacked, a[0] and a[n-1] are exchanged, and then the heap is restored again to a[0...n-2. The second time will a[0] and A[n–2] exchange, and then to a[0...n-3] again restore the heap, repeated this operation until A[0] and a[1] Exchange. Because each time the smallest data is incorporated into the subsequent ordered interval, the entire array is ordered after the operation is complete. A bit similar to the Direct selection sort .

void Minheapsorttodescendarray (int a[], int n) {for (int i = n-1; I >= 1; i--) {Swap (A[i], a[0]); Minheapfixdown (A, 0, i);}}

Note that using the minimum heap sort is a descending array, to get an incremented array, to be able to use the maximum heap.

Because the time complexity of the recovery heap is O (logn) each time again, the N-1 time and again the heap operation is resumed, plus the N/2-down adjustment of the previous build heap, and the complexity of each adjustment is O (logn). Two operation times added or O (N * logn). Therefore, the time complexity of heap sequencing is O (N * logn). STL also implements the related functions of the heap, and can refer to thefour heap heap of STL series.

Note 1 As a data structure, it is best to use classes to encapsulate their data and methods so that they are easy to operate and understandable. In addition, in addition to heap sorting to use the heap, there are many occasions to use the heap to facilitate and efficient processing of data, will be introduced in the future.

Reprint please indicate source, original address: http://blog.csdn.net/morewindows/article/details/6709644

Seven-heap and heap ordering of vernacular classical algorithm series

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.