Skip to main content
3 of 3
Improved formatting, grammar
Ivaylo Valchev
  • 10.4k
  • 6
  • 60
  • 86

Why priority queue is implemented using heaps when we can implement it using just vector more efficiently

Why is priority_queue implemented using heaps when without using heaps too we can implement it with just vectors.

Suppose we use vector as a queue and keep the elements in decreasing order. We can use this as priority queue.

For insertion: We can use binary search. Complexity O(logN)

For deletion: Here also we can use binary search. Complexity O(logN)

For top element: O(1)

In addition, we can have access kth maximum element in just O(1) time which is not the case with heaps.

Then, why do we use heaps for implementing priority queues?