std::sort_heap 如何破坏堆属性?

huangapple go评论63阅读模式
英文:

How does std::sort_heap break the heap property?

问题

cppreference.com说:

结果范围不再具有堆属性。

cplusplus.com说:

范围失去了作为堆的属性。

它们的意思是,排序后的范围不再满足堆的性质。

英文:

cppreference.com says:

> The resulting range no longer has the heap property.

cplusplus.com says:

> The range loses its properties as a heap.

What do they mean? It sorts, and sorted implies heap, no?

答案1

得分: 3

一个升序排序的列表自动成为最小堆。一个降序排序的列表自动成为最大堆。std::sort_heap 将一个最大堆转换为一个升序排序的列表,这不再是一个最大堆(但它是一个最小堆)。再强调一遍:关键点在于排序顺序被颠倒。列表从“弱降序”(最大堆)转变为升序(排序后,不再是最大堆)。

英文:

A list sorted ascending is automatically a min-heap. A list sorted descending is automatically a max-heap. std::sort_heap turns a max heap into a list sorted ascending, which is no longer a max-heap (but it is a min-heap). To reiterate: the subtlety is that the ordering is being reversed. The list is turned from "weakly descending" (max-heap) to ascending (sorted, not a max-heap).

huangapple
  • 本文由 发表于 2023年5月7日 05:51:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定