PriorityQueue在每次调用get时都调用sorted吗?

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

Does PriorityQueue call sorted every time get is called?

问题

queue.PriorityQueue的文档说:

"最低值的条目首先被检索(最低值的条目是由sorted(list(entries))[0]返回的)。"

这是否意味着每次我使用PriorityQueueget方法时元素都会被重新排序?这似乎是低效的。

英文:

The docs for queue.PriorityQueue say:

> The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]).

Does that mean that every time I use the get method of a PriorityQueue that the elements are re-sorted? That would seem inefficient.

答案1

得分: 0

The elements of a PriorityQueue are stored in a binary heap data structure, which allows for efficient retrieval of the smallest element when you call the method.

So when sorting is done in such a way that the lowest valued elements are always at the top of the heap. That's why you will get the smallest value first.

You can refer to example and Explanation in depth

英文:

> That does not means that the elements are re-sorted every time when
> you use the get method.

The elements of a PriorityQueue are stored in a binary heap data structure, which allows for efficient retrieval of the smallest element when you call the method.

So when sorting is done in such a way that the lowest valued elements are always at the top of the heap. That's why you will get smallest value on first.

You can refer to example and Explanation in deeply

huangapple
  • 本文由 发表于 2023年2月10日 13:11:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407200.html
匿名

发表评论

匿名网友

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

确定