Open In App

Priority Queue Using Array

Last Updated : 07 Mar, 2025
Summarize
Comments
Improve
Suggest changes
Share
2 Likes
Like
Report

A priority queue is a data structure that stores elements with associated priorities. In a priority queue, elements are dequeued in order of their priority, with the highest priority elements being removed first. It is commonly used in algorithms like Dijkstra's for shortest path and in real-time scheduling tasks. Priority queues can be implemented using arrays, heaps, or linked lists.

Examples:

Input: Values = 4 (priority = 1), 5 (priority = 2), 6 (priority = 3), 7 (priority = 0)
Output: 7 4 5 6 (In descending order of priority, starting from the highest priority).
Explanation: The elements are stored in an array sorted by priority.
When you perform a dequeue(), the highest priority element (7 with priority 0) is removed first.
The next highest priority element (4 with priority 1) is then at the front of the array, followed by the others.

Input: Values = 10 (priority = 3), 3 (priority = 1), 8 (priority = 2), 1 (priority = 0)
Output: 1 3 8 10 (In descending order of priority)
Explanation: The array will be sorted by priority: 1 (highest priority) comes first, followed by 3, 8, and 10. After calling dequeue(), the element with the highest priority, 1, is removed first, then 3, followed by 8, and finally 10.

Priority Queue using Array

A priority queue stores elements where each element has a priority associated with it. In an array-based priority queue, elements are ordered so that the highest priority element is always at the front of the array. The array is sorted according to the priority values, with the element with the lowest priority value (highest priority) placed at the front.

Operations:

1. enqueue(value, priority): This function inserts a new element into the queue by adding it to the end of the array. The element is appended without any sorting. The queue remains unsorted until the peek() or dequeue() function is called, which scans the entire array to find and handle the element with the highest priority based on the lowest priority value.

2. dequeue(): This function removes and returns the element with the highest priority (i.e., the element with the lowest priority value). It locates the element with the highest priority using the peek() function, then removes it by shifting the remaining elements.

3. peek(): This function returns the element with the highest priority (the element with the smallest priority value) without removing it from the queue. It scans the array to find the element with the lowest priority value and returns its index.

C++
#include <bits/stdc++.h>
using namespace std;

struct Item {
    int val, priority;
};

// To store priority queue items 
vector<Item> pq;

// Function to insert element in priority queue
void enqueue(int val, int priority) {
    pq.push_back({val, priority});
}

// Function to get index of element with
// highest priority
int peek() {
    int ind = -1, maxPriority = INT_MIN;
    for (int i = 0; i < pq.size(); i++) {
        
        // Update index if a higher priority 
        // is found
        if (pq[i].priority > maxPriority || 
           (pq[i].priority == maxPriority && pq[i].val > pq[ind].val)) {
            maxPriority = pq[i].priority;
            ind = i;
        }
    }
    return ind;
}

// Function to remove the element with highest priority
void dequeue() {
    int ind = peek(); // Get index of highest priority element
    if (ind != -1) pq.erase(pq.begin() + ind);
}

int main() {
    enqueue(10, 2);
    enqueue(14, 4);
    enqueue(16, 4);
    enqueue(12, 3);

    cout << pq[peek()].val << endl; 
    dequeue();

    cout << pq[peek()].val << endl; 
    dequeue();

    cout << pq[peek()].val << endl;
    return 0;
}
C Java Python C# JavaScript

Output
16
14
12

Time Complexity:

  • enqueue(value, priority): O(1) (since it's just an insertion).
  • peek(): O(n) (iterating through the entire array).
  • dequeue(): O(n) (finding the highest-priority element and shifting elements).

Auxiliary Space: The auxiliary space complexity for this program is O(1)

Related Articles:

What is Priority Queue | Introduction to Priority Queue
Priority Queue using Linked List
Priority Queue using Binary Heap
Why is Binary Heap Preferred over BST for Priority Queue ?


Article Tags :
Practice Tags :

Similar Reads