Heap and heap sorting-priority queue and heap sorting priority queue

Source: Internet
Author: User

Heap and heap sorting-priority queue and heap sorting priority queue

In the previous section, we wrote about trees and Binary Trees.

Http://blog.csdn.net/wtyvhreal/article/details/43487095


Heap is a special full binary tree.


All parent nodes are smaller than child nodes. Such a Complete Binary Tree is called the minimum heap, and vice versa.


To change a Complete Binary Tree to a minimum heap step:


The code for downward adjustment is as follows:


The preceding figure shows that the time complexity of adjusting the heap is O (logN ).


If the heap size is N, it takes O (logN) to insert a new element ).

Insert the value 3 in the minimum heap.

The code for upward adjustment is as follows:


Heap building: Starting from an empty heap, insert each element into the heap in sequence. The overall time complexity is O (NlogN ).


The following uses the O (N) Complexity to create a heap.

...


Create n elements:

First, the n nodes can be encoded from 1 to n in the top-down, left-right mode, so that the n nodes can be converted into a Complete Binary Tree, then, from (the last non-leaf node) to the n/2 nodes (n indicates the total number of nodes) start to process the Complete Binary Tree one by one at the root node (the last non-leaf node is the nth/second node ). Adjust the current node downward as needed until it meets the heap features. The time complexity is O (N ).

Prove that the time complexity is O (N ):


First, this loop is from I = n/2-> 1, that is, it is a bottom-up heap. As a result, 1/2 of the elements are compared down once, 1/4 is compared down twice, and 1/8 is compared down three times ,......, 1/2 ^ k is compared down k times, of which 1/2 ^ k <= 1, k is approximately equal to lg (n ). So there is a total amount of comparisons:

T = () * n

So S =

1/2 S =
S-1/2 S = 1/2 S =
This step is obvious, S <= 2
So T <= 2n => T = O (n ).



The time complexity of heap sorting is also O (NlogN ).

For example, to sort data in ascending order, create a minimum heap first. Then, each time you delete the top element, adjust it until the heap is empty.



Another method is to create the largest heap instead of the smallest heap created above.

After the maximum heap is established, the largest element is h [1], and h [1] and h [n] are exchanged. h [n] is the largest element. Then, the switching h [1] is adjusted downward to keep the heap features. Switch once, n --, until the heap size changes to 1. At this time, the h array is sorted.


Summary:

Priority queue is a data structure that supports inserting elements and finding the most value. Heap is a typePriority queue.


The heap is often used to find the maximum K number in a series. You only need to create a minimum heap with a size of K. The heap top is the maximum K number,Time complexity O (NlogK).

(The number of 10 is 3rd. First, the number of any three is used to create the minimum heap. Then, starting from the number of 4th, it is smaller than the heap top. It is larger than the heap top, the current heap top is discarded and the number is used as the new heap top, and then maintained ...)



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.