Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Does PriorityQueue call sorted every time get is called?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading