Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Is heap sorting a stable sorting algorithm?

Shulou Source: shulou.com Published: 2022-06-02 22:57:09 10月19日 Update

Today, I will talk to you about whether heap sorting is a stable sorting algorithm. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Heap sorting, quick sorting, Hill sorting and direct selection sorting are unstable sorting algorithms, while cardinality sorting, bubbling sorting, direct insertion sorting, half insertion sorting and merge sorting are stable sorting algorithms.

Heap sort

We know that the structure of the heap is that the children of node I are 2 nodes and 2*i+1 nodes, the large top heap requires the parent node to be greater than or equal to its 2 child nodes, and the small top heap requires the parent node to be less than or equal to its 2 child nodes.

In a sequence with a length of n, the process of heap sorting is to choose the largest (large top heap) or the smallest (small top heap) with three values of its child nodes starting from n / 2. of course, the choice between these three elements will not destroy the stability. But when the elements are selected for the parent nodes 2-1, 2-1 and 2-2,.. .1, the stability will be destroyed.

It is possible that the n / 2 parent node exchange swaps the latter element, while the n / 2-1 parent node does not swap the latter same element, then the stability between the two same elements is destroyed. Therefore, heap sorting is not a stable sorting algorithm.

After reading the above, do you have any further understanding that heap sorting is a stable sorting algorithm? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Sorting nodes algorithms elements selection content stability same size between maximum minimum cardinality children sequence more knowledge article structure industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Information Xiaomi NVidia Huawei