英文:
Does PriorityQueue call sorted every time get is called?
问题
queue.PriorityQueue
的文档说:
"最低值的条目首先被检索(最低值的条目是由sorted(list(entries))[0]
返回的)。"
这是否意味着每次我使用PriorityQueue
的get
方法时元素都会被重新排序?这似乎是低效的。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论