TutorChase logo
Login
AQA A-Level Computer Science

11.2.3 Priority queues

Priority queues are abstract data structures where elements are processed based on priority, not the order of arrival. They’re crucial in scheduling and decision-making.

What is a priority queue?

A priority queue is a data structure that manages a collection of elements, each associated with a priority value. Unlike a standard linear queue, which follows a First-In-First-Out (FIFO) model, a priority queue removes elements based on their priority level. The element with the highest priority is always dequeued first, regardless of when it was added.

If multiple elements share the same priority, the one added earliest is removed first, preserving FIFO behaviour within that priority level.

Priority queues are essential in situations where some tasks or elements are more urgent than others and should therefore be processed sooner. This is different from linear or circular queues, which rely strictly on order of arrival and do not account for urgency.

Key characteristics

  • Each element is stored with a priority value (usually an integer).

  • Higher priority values indicate more urgent tasks.

  • Elements with equal priority are dequeued in the order they were inserted.

  • Supports common operations: enqueue (add), dequeue (remove highest priority), isEmpty, and isFull (in fixed-size implementations).

How it differs from a linear queue

Take your grades to the next level!

UPGRADING TO PREMIUM UNLOCKS
AI Tutor
AI-powered study assistant
instant feedback and guidance
Predicted Papers
Examiner-style predicted papers
based on recent exam trends
Practice Questions
All exam practice questions
by topic for each subject
Study Notes
All detailed revision notes
written by expert teachers
Cheat Sheets
Quick revision summaries
perfect for last-minute review
Past Papers
Complete collection
of practice and past exam papers
Email
Password
Confirm Password
Already have an account?

Practice Questions

FAQ

Yes, a priority queue can accept negative or non-integer priority values, such as floating-point numbers, as long as there is a consistent and defined method for comparing them. The priority value simply needs to support a comparison operation (e.g. greater than or less than). This allows developers to rank items using any numerical scale, including time estimates, costs, or urgency levels. Negative values may be used in systems where a lower value indicates a higher priority, such as scheduling tasks with shorter remaining execution times. In floating-point systems, care must be taken to handle rounding errors or near-equal comparisons precisely, as floating-point arithmetic can introduce subtle bugs in comparisons. Most programming languages with built-in priority queue support (like Python’s heapq) rely on comparison logic, so as long as the values are comparable, the implementation will work. However, using non-integer priorities may slightly affect performance depending on how sorting or heap balancing is handled internally.

When a priority queue encounters multiple items with the same priority value, the standard approach is to preserve the insertion order among those items, ensuring FIFO behaviour within the same priority level. This is typically managed by recording the timestamp or sequence number of insertion, especially in implementations like heaps or sorted arrays. For example, if Task A and Task B both have a priority of 3 and Task A was inserted first, it will be dequeued before Task B. This ensures fairness and predictability, which is particularly important in systems like customer service platforms or CPU schedulers. However, not all basic implementations automatically preserve insertion order. In languages or libraries that use heaps without order tracking, such as a binary min-heap, equal-priority items might be dequeued in a non-deterministic order unless additional tie-breaking data is included. To guarantee consistent behaviour, a secondary attribute (like timestamp or sequence index) should be incorporated during insertion.

Priority queues are widely used in real-time systems, but their suitability depends on how they are implemented and the system’s time constraints. In real-time environments, tasks must be executed within strict deadlines. A heap-based priority queue is often preferred because it offers a balanced performance: both insertion and removal operations have logarithmic time complexity (O(log n)), which is efficient for maintaining sorted priority order. For soft real-time systems (like video streaming), this is typically sufficient. However, in hard real-time systems, such as flight control or medical devices, even small timing variations can be critical. In such cases, priority queues must be carefully managed, possibly with pre-allocated memory and real-time scheduling algorithms that limit latency. Dynamic memory allocation should be avoided to reduce unpredictable delays. Additionally, the choice of data structure must ensure predictable and bounded performance, which is more important than average-case efficiency. Therefore, while priority queues can be used in real-time systems, careful implementation is crucial.

When two items share the same priority but need to be processed differently, it’s a signal that priority alone is not sufficient to determine execution order. To handle this, many systems introduce secondary sorting criteria or use multi-level priority queues. A secondary criterion could be a timestamp, a job type, or an urgency flag, allowing the system to further differentiate items with the same numeric priority. For instance, if Task A and Task B both have priority 4, but Task A must complete sooner due to dependencies, an additional attribute (like a deadline) can guide the queue’s logic. Alternatively, a multi-level queue can be used, where each level has its own internal queue or rules. In practice, developers may extend the priority queue structure by storing tuples like (priority, time_created, task_type) so that sorting and retrieval respect all relevant attributes. This technique ensures the queue behaves predictably, even when priority alone isn’t a reliable differentiator.

Yes, priority queues can work with non-numeric priorities, such as strings or custom objects, provided the implementation includes a way to compare these values meaningfully. For instance, strings like "high", "medium", and "low" can be mapped to numeric equivalents (e.g. 3, 2, 1) before being used in the queue. Alternatively, a custom comparator function can define the logic for comparing objects based on their attributes. For example, a Task object might contain a priority_level and a deadline, and the comparator would use these fields to rank tasks. In languages like Java or Python, priority queues often allow users to supply custom comparison logic, enabling full flexibility in sorting. However, using non-numeric priorities increases complexity and the potential for inconsistencies, especially if comparison rules are ambiguous or not well defined. Therefore, while it’s entirely feasible to use custom or symbolic priorities, they must be internally standardised or mapped to numerical values for consistent and efficient queue operations.

Hire a tutor

Please fill out the form and we'll find a tutor for you.

1/2
Your details
Alternatively contact us via
WhatsApp, Phone Call, or Email