Coding & data structures · Sorting and selection — order, and the parts of it you actually need

Heaps: paying only for the order you need

A heap is the structure you get by giving up almost all of sorting's guarantees and keeping one — the extreme element is at the front — and that single retained guarantee is enough for top-k, k-way merge, running medians, schedulers, and Dijkstra, at O(log n) per update instead of a re-sort.

28 min read Full lesson in the app Patterns: binary heap, sift up and sift down, bottom-up heapify, bounded heap of size k, two heaps

What you'll be able to do

Before this: sorting-and-comparators, stacks-and-queues

Log in to read the rest of this lesson

A heap is the structure you get by giving up almost all of sorting's guarantees and keeping one — the extreme element is at the front — and that single retained guarantee is enough for top-k, k-way merge, running medians, schedulers, and Dijkstra, at O(log n) per update instead of a re-sort. This walkthrough runs about 28 minutes, with runnable code you can edit and re-run as you read. A new account gets 7 days of Pro free, no card required.

Log in to continue

The first module of every track is free to read on the web: see what's open in Coding & data structures.