Heap (priority queue) and heap sorting popular introduction (c++ implementation)

0 Introduction

Heap is a more sophisticated data structure. The binary heap generally used is essentially a complete binary tree, so it is generally possible to simulate various operations of the heap by using an array. For any node, if its node number is iii( 1 < = i < = n 1<=i <= n 1<=i<=n ), then its left child node number is2 i 2i2 i , the right son node number is2 i + 1 2i+12 i+1 , the parent node isi / 2 i/2i / 2 . In the minimum heap, the value of each node is smaller than the parent node and larger than its son node. The heap generally supports operations such as inserting elements, returning, and deleting the most valued elements. The following two operations are described using the binary minimum heap as an example.

1 heap operation

1.1 Insert elements

Inserting an element into the heap is very simple. For a new element x, if the current heap size is size, first put the element at the end of the array,, h[++size] = x;and then perform the filtering operation. The operation of up-filtering is as follows, that is to say, if its parent node is greater than it, then it is known that the element can be exchanged up.

void up(int u)
{
    
    
    while (u / 2 && h[u] < h[u / 2])
    {
    
    
        swap(h[u], h[u / 2]);
        u /= 2;
    }
}

1.2 Delete the most value element

The first element of the smallest heap is always the smallest element, so the idea of ​​deleting the most value element is this, so that the first element of the array is equal to the last element, if the current heap size is size, size–, then the first element Only one element can be filtered down. The so-called filtering operation, if the current node is found to be larger than its son node, it will be exchanged with the child node until it reaches the correct position. The code is shown below.

void down(int u)
{
    
    
    int t = u;
    if (u * 2 <= size && h[u * 2] < h[t]) t = u * 2;
    if (u * 2 + 1 <= size && h[u * 2 + 1] < h[t]) t = u * 2 + 1;
    if (u != t)
    {
    
    
        swap(h[u], h[t]);
        down(t);
    }
}

1.3 Build a pile

A common idea of ​​building a heap is to continuously insert array elements into a heap. In the worst case, the algorithm complexity will reach nlogn nlogn.n l o g n . There is a kind ofo (n) o(n)The heap building algorithm of o ( n ) is shown below, only fromn / 2 n/2Start at n / 2 , and filter continuously from back to front.

for (int i = n / 2; i; i -- ) 
	down(i);

2 heap sort

According to the three operations mentioned above, we can complete the heap sort. For the input array, we first build a heap, then loop to output the smallest element, and delete the smallest element. Take acwing838 as an example. The subject requirements are as follows: the
Insert picture description here
implementation code is as follows:

#include <stdio.h>
#include <algorithm>

using namespace std;

int mySize;
const int N = 1e5+10;
int nums[N];

void down(int i)
{
    
    
    int d = i;
    if(2*i <= mySize && nums[d] > nums[2*i]) d = 2*i;
    if(2*i+1 <= mySize && nums[d] > nums[2*i+1]) d = 2*i + 1;
    if(i != d)
    {
    
    
        swap(nums[i],nums[d]);
        down(d);
    }
}

int main(void)
{
    
    
    int n,m;
    scanf("%d%d",&n,&m);
    mySize = n;
    for(int i = 1; i <= n; i++)
        scanf("%d",&nums[i]);
    for(int i = n/2; i ; i--)
        down(i);
    for(int i = 1; i <= m; i++)
    {
    
    
        printf("%d ",nums[1]);
        nums[1] = nums[mySize--];
        down(1);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/MoonWisher_liang/article/details/108711146