Heap Sort, priority queue

Source: Internet
Author: User

1. Heap Sequencing

A. Definition of a heap

The n element sequence {k1,k2,..., kn} is called a heap when and only if the following relationships are met.

Ki<=k2i and ki<=k2i+1  (Keng Gen) ki>=k2i and ki>=k2i+1 (Dagen)

The following is for the maximum heap

B. Nature of the maintenance heap

Max-heapify the sub-tree with the root node of I as the maximum heap property by letting the value of a[i] "step down" in the maximum heap (the value of a[i] is less than the value of its left and right children).

max-heapify (a,i)    = left (i)    = right (i)    if l <= a.heap-size and A[l] > a[i]        = L    else largest = I    if r <= a.heap-size and A[r] > a[largest]        = R    if  largest≠i        swap (A[i],a[largest] )        Max-heapify (a,largest)

Complexity of Time: O (LGN)

C. Build a heap

The elements in sub-array A (N/2+1..N) are the leaf nodes of the tree, and each leaf node can be seen as a heap containing only one element. Build-max-heap calls the max-heapify once for other nodes in the tree.

build-max-Heap (A)    a.heap-size = a.length    for i = a.length/21          Max-heapify (a,i)

Time complexity: O (N)

D. Heap sequencing

heap-Sort (a)    Build-max-Heap (a)    for2        Swap (a[  1],a[i])        a.heap-size = a.heap-size-1        Max-heapify (A, 1)

Complexity of Time: O (N*LGN)

2. Priority queue

The specific algorithm will not talk about, to see how the priority queue in C + + is used.

 in header <queue>template<    class  T,    class Container = Std::vector<t>,    classclass priority_queue;

The default is the maximum priority queue. You can provide the compare parameter to achieve the maximum priority queue.

Compare = greater<t>

Example:

#include <functional>#include<queue>#include<vector>#include<iostream>Template<typename t>voidPrint_queue (t&q) { while(!Q.empty ()) {Std::cout<< q.top () <<" ";    Q.pop (); } std::cout<<'\ n';} intMain () {std::p riority_queue<int>Q;  for(intN: {1,8,5,6,3,4,0,9,3,2}) Q.push (n);     Print_queue (q); std::p riority_queue<int, std::vector<int, std::greater<int> >Q2;  for(intN: {1,8,5,6,3,4,0,9,3,2}) Q2.push (n); Print_queue (q2);}
View Code

For custom types

If the comparison operator is overloaded

priority_queue<t> q// Max priority queue priority_queue<t,vector<t>,greater<t>> Q // Minimum priority queue

If you do not have an overloaded comparison operator, you need to provide a comparator passed to the Compare parameter

//Maximum priority queue Comparatorstructcomp{BOOL operator() (T &a,t &b)Const {        returnA.key <B.key; }};//minimum priority Queue Comparatorstructcomp{BOOL operator() (T &a,t &b)Const {        returnA.key >B.key; }};//UsePriority_queue<t,vector<t>,comp> Q

Reference:

Heap Sort, priority queue

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.