Skip to content
Commitwise
Menu

Priority queue

Also called: Heap, Min-heap, Max-heap

A collection that always hands out the smallest (or largest) element next, usually built on a heap.

Definition

A priority queue supports adding elements and removing the one with the highest priority, both in O(log n), and peeking at it in O(1). It is usually implemented as a binary heap stored in an array. Java and Kotlin have PriorityQueue (a min-heap by default); Swift uses Heap from swift-collections. Its iteration order is not sorted.

In practice

A scheduler keeps tasks in a priority queue ordered by run time and always starts the earliest one.

Learn it in context

Lessons that use this term.

Related terms

Esc

Type to search the whole site.