Sorting

Problem shape

Intermediate
4
8
15
16
23
42
Core idea

Build a heap, then repeatedly move the max value to the end.

Step 1 / 5
Problem shape
Current lesson

Heap Sort Lab

Sorting Intermediate

Heap Sort

Welcome to the Heap Sort algorithm lab. Build a heap, then repeatedly move the max value to the end. Use it when: Use when O(1) auxiliary space and guaranteed O(n log n) time are useful. The time complexity is O(n log n), and the space complexity is O(1). The pseudocode is: build max heap for end from n - 1 down to 1 swap heap[0], heap[end] heapify(0, end) As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(n log n)

Space

O(1)

Pseudocode

build max heap
for end from n - 1 down to 1
  swap heap[0], heap[end]
  heapify(0, end)
Open deeper structure lesson
1.0x