Python algorithm: Heap Sort (Heap Sort) principle and algorithm implementation, TopK problem

.Heap Sort

Heap sort (English: Heapsort) refers to a sorting algorithm designed using the data structure of the heap. The heap is a structure that approximates a complete binary tree, and at the same time satisfies the nature of the heap: that is, the key value or index of a child node is always smaller (or larger) than its parent node.

The heap can be seen as a "complete binary tree" structure:

According to the relationship between parent and child nodes, the heap can be roughly divided into two categories (as shown in the figure below):

Big root heap/big top heap: the value of each node is greater than or equal to the value of its left and right child nodes;

Small root heap/small top heap: the value of each node is less than or equal to the value of its left and right child nodes.

Heap and sort:

case:

Assuming that there is an array nums=[5,2,1,9,6,8] to be sorted as an example, I will explain how to construct a large root heap and realize its "ascending order".

I. Construct a large root pile

How to construct a complete binary tree into a large top heap?

A good way to implement it is to start from the subtree whose root node is the last "non-leaf node", and perform adjustment operations from right to left and bottom to top.

For a subtree with a non-leaf node, its basic adjustment operations include:

1. If the node is greater than or equal to its left and right child nodes, there is no need to adjust the node, because its subtrees are already heap-ordered;

2. If the node is smaller than the larger of its left and right child nodes, exchange the node with the larger of the child nodes, and continue the adjustment operation starting from the position of the larger one until the heap is in order .

For nums=[9,6,8,2,5,1], the basic steps of heap sorting are as follows:

1、

2、

3、

4、

At this point, all adjustments are completed, and we have established a large root pile:

After building a large root heap, the elements in the array can be sorted. To sum up, the top element of the heap is exchanged with the end element, and the end is the maximum value at this time. After removing the end element, reconstruct the other n−1 elements into a large root heap, and continue to exchange the top element and the end element of the heap, so that the second largest value among the n elements of the original array can be obtained. By repeating exchange, reconstruction, exchange, and reconstruction in this way, an array in "ascending order" can be obtained.

Maximum element: At this time, the top element of the heap is the maximum value, and it is exchanged to the end, as shown below:

After the exchange is completed, the largest element at the end is removed. At this time, the heap needs to be rebuilt so that the remaining elements continue to meet the requirements of the large root heap. As follows:

次大元素:此时堆顶元素为待排序元素中的最大值(即原数组中的次大值),将堆顶元素交换到末尾,如下所示:

交换完成后,除去末尾最大元素,此时需要对堆进行重建,使得剩余元素继续满足大根堆的要求(省略)。

第三大元素:此时堆顶元素为待排序元素中的最大值(即原数组中的第三大值),将堆顶元素交换到末尾,如下所示:

交换完成后,除去末尾最大元素,此时需要对堆进行重建,使得剩余元素继续满足大根堆的要求(省略)。

第四大元素:此时堆顶元素为待排序元素中的最大值(即原数组中的第四大值),将堆顶元素交换到末尾,如下所示:

交换完成后,除去末尾最大元素,此时需要对堆进行重建,使得剩余元素继续满足大根堆的要求(省略)。

次小元素(第五大元素):此时堆顶元素为待排序元素中的最大值(即原数组中的次小元素或第五大元素),将堆顶元素交换到末尾,如下所示:

交换完成后,除去末尾最大元素,此时堆中仅剩一个元素,即为原数组中的最小值。

至此,基于大根堆的升序排列完成,如下所示:

下面给出了 Python 语言的实现:

def Max_heap(li,low,high):#建立最大堆
    i = low
    j = 2 * i +1
    temp = li[low]
    while j < high :
        if li[j] < li[j+1] and j+1<high:
            j += 1
        if li[j] > li[i]:
            li[i],li[j] = li[j],li[i]
            i = j
            j = 2 * i + 1
        else :
            i = j
            j = 2 * i + 1
def heap_sort(li):#实现排序
    new_li=[]
    for j in range(n - 1, -1, -1):
        new_li.append(li[0])#记录最大元素

        li[j], li[0] = li[0], li[j]


        Max_heap(li,0,j-1)

        print(new_li)#每次记录都打印下来,也可以调整到后面打印最后结果


li = [2,4,1,6,8,2,7,9,3,10,5]
n = len(li)
for i in range(n//2-1, -1, -1):         # 从第一个非叶子节点n//2-1开始依次往上进行建堆的调整
            Max_heap(li, i, n-1)

heap_sort(li)

Guess you like

Origin blog.csdn.net/weixin_53374931/article/details/129578930
Recommended