Task prioritization decides what should happen next when tasks do not deserve equal attention. Unlike a simple queue, which serves the oldest item first, a priority queue serves the item with the highest priority. An emergency room sees the most urgent patient first; a bug system handles critical fixes before desired improvements. section 10.1 section 10.2
A prioritized queue usually provides two core operations:
- Insert a new task, placing it according to its priority rather than automatically at the rear. section 10.2 section 10.3
- Top, or an equivalent operation such as pull or extract_max, return and remove the highest-priority task. section 10.3
- Define priority explicitly: a higher value can mean greater urgency, while another function can make smaller values appear first. section 10.13 section 10.16
Several structures can implement prioritization. A sorted linked list or sorted array keeps tasks ordered so top is quick, but insertion must preserve that order. A heap offers the usual priority-queue implementation: its root always holds the highest-priority element, while the remaining elements need not be fully sorted. A heap can use an array internally and can be built from an existing array with heapify. section 10.4 section 10.5 section 10.16
Prioritizing bug fixes
A maintenance queue labels fixes as desired, needed, urgent, or critical. When a developer asks for the next bug, the system returns the most urgent item instead of the oldest one. The priority value can be a decimal number, with larger numbers indicating greater urgency. section 10.2 section 10.16
class Heap:
def __init__(self, elements=None, element_priority=lambda x: x):
self._priority = element_priority
if elements is not None and len(elements) > 0:
self._heapify(elements)
else:
self._elements = []
Use the compact Heap constructor to see how an element-priority function and optional heapify step make prioritization configurable. section 10.16




