简体   繁体   English

为什么堆排序不被视为稳定的排序算法

[英]Why Heap sort is not considered as a stable sorting algorithm

In Java Heap sort seems a best sorting algorithm while sorting an array of random numbers according to http://www.sorting-algorithms.com/ But still I read that Heap sort is not stable, why so? 在Java中,堆排序似乎是一种最佳的排序算法,同时根据http://www.sorting-algorithms.com/对随机数数组进行排序,但是我仍然读到堆排序不稳定,为什么呢? Which sorting algorithm should be considered best algorithm while sorting an array or random numbers? 在对数组或随机数进行排序时,应将哪种排序算法视为最佳算法?

Because it does not guarantee that when it encounters 2 equal elements in the collection that is being sorted, that their position will be preserved. 因为它不能保证当它在要排序的集合中遇到2个相等的元素时,将保留它们的位置。 Let's say this collection: 3 2 2 1 . 假设这个集合: 3 2 2 1 When this algorithm will encounter 2 and compare it with 2, it may reverse them. 当此算法遇到2并将其与2进行比较时,可能会将它们取反。 Sorting algorithms stability . 排序算法的稳定性 By the way, in case you don't care about the order of identical items in your collection, then you may chose the fastest one. 顺便说一句,如果您不关心集合中相同项目的顺序,则可以选择最快的项目。

You should read this . 你应该读这个

Heap sort uses a binary heap, the data structure and algorithm used makes heapsort unstable. 堆排序使用二进制堆,所使用的数据结构和算法使堆排序不稳定。

Stable Sort: A sort which doesn't change the relative position of same/equal elements. 稳定排序:一种不会更改相同/相等元素的相对位置的排序。

For example, 例如,
I/P: 2, 4, 3(a), 5, 1, 3(b) I / P:2、4、3(a),5、1、3(b)
O/P: 1, 2, 3(a), 3(b), 4, 5 O / P:1、2、3(a),3(b),4、5

In I/P 3(b) comes after 3(a) and the same stays intact in the O/P. 在I / P中,3(b)在3(a)之后,并且在O / P中保持不变。

It can be explained very easily. 这很容易解释。 Let us take the following example: 让我们以以下示例为例:

3,3,2,1

Consider the first 3 to be 3(a) and second 3 to be 3(b). 考虑第一个3为3(a),第二个3为3(b)。

3(a),3(b),2,1

Heapsort begins by extracting the maximum number from the max-heap, which is the first element and then putting it on the last position. 堆排序首先从最大堆中提取最大数目(这是第一个元素),然后将其放在最后一个位置。

3(b),2,1,3(a)

Then size is decreased by 1 and a heapify operation is applied.Therefore the new size is 3 and the first three elements already satisfy the heap property. 然后将大小减小1并应用heapify操作。因此新大小为3并且前三个元素已经满足了heap属性。

Now comes the step which show that heapsort is not stable . 现在出现表明堆排序不稳定的步骤

Again we will extract the maximum from the heap which is the first element and put it in last position. 再次,我们将从堆中提取最大值(第一个元素)并将其放在最后一个位置。

But because size is now 3, the last position is left of 3(a) and hence we get: 但是由于大小现在为3,所以最后一个位置在3(a)的左边,因此我们得到:

2,1,3(b),3(a)

As we can see that order of the elements 3(a) and 3(b) is reversed from the original order and remaining part of the heapsort will only sort the first two elements and therefore, the elements 3(a) and 3(b) will remain intact at their positions. 如我们所见,元素3(a)和3(b)的顺序与原始顺序相反,并且堆排序的其余部分将仅对前两个元素进行排序,因此,元素3(a)和3(b) )将保持原样。 This is the reason that heapsort is not stable. 这就是heapsort不稳定的原因。

The final result is 最终结果是

1,2,3(b),3(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM