Priority Queue is an extension of queue with following properties.
- Every item has a priority associated with it.
- An element with high priority is dequeued before an element with low priority.
- If two elements have the same priority, they are served according to their order in the queue.
In the below priority queue, element with maximum ASCII value will have the highest priority.

A typical priority queue supports following operations.
insert(item, priority): Inserts an item with given priority.
getHighestPriority(): Returns the highest priority item.
deleteHighestPriority(): Removes the highest priority item.
How to implement priority queue?
Using Array: A simple implementation is to use array of following structure.
struct item {
int item;
int priority;
}
insert() operation can be implemented by adding an item at end of array in O(1) time.
getHighestPriority() operation can be implemented by linearly searching the highest priority item in array. This operation takes O(n) time.
deleteHighestPriority() operation can be implemented by first linearly searching an item, then removing the item by moving all subsequent items one position back.
We can also use Linked List, time complexity of all operations with linked list remains same as array. The advantage with linked list is deleteHighestPriority() can be more efficient as we don’t have to move items.
Using Heaps:
Heap is generally preferred for priority queue implementation because heaps provide better performance compared arrays or linked list. In a Binary Heap, getHighestPriority() can be implemented in O(1) time, insert() can be implemented in O(Logn) time and deleteHighestPriority() can also be implemented in O(Logn) time.
With Fibonacci heap, insert() and getHighestPriority() can be implemented in O(1) amortized time and deleteHighestPriority() can be implemented in O(Logn) amortized time.
Applications of Priority Queue:
1) CPU Scheduling
2) Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum Spanning Tree, etc
3) All queue applications where priority is involved.
A priority queue is implemented using Heap. Please refer below articles for our own implementation and library implementations.
- Binary Heap (The most common implementation of priority queue)
- Priority Queue in C++.
- Priority Queue in Java.
- Priority Queue in Python.
- Priority Queue in JavaScript.
Useful Links :
References:
http://en.wikipedia.org/wiki/Priority_queue
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Priority Queue using Queue and Heapdict module in Python
- Difference between Circular Queue and Priority Queue
- Why is Binary Heap Preferred over BST for Priority Queue?
- Applications of Priority Queue
- How to implement stack using priority queue or heap?
- Implementation of Priority Queue in Javascript
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Priority Queue in Python
- Double ended priority queue
- Huffman Coding using Priority Queue
- Priority Queue of Vectors in C++ STL with Examples
- CPU Scheduling in Operating Systems using priority queue with gantt chart
- Priority queue of pairs in C++ with ordering by first and second element
- Find the K closest points to origin using Priority Queue
- Priority Queue using Binary Heap
- Heap and Priority Queue using heapq module in Python
- Priority Queue in C++ Standard Template Library (STL)
- Stack and Queue in Python using queue Module
- Check if a queue can be sorted into another queue using a stack

