Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Heap Sort proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Build a heap and repeatedly extract extrema for in-place O(n log n) sorting. Learn the algorithm's preconditions, state transition, correctness argument, Python cost model, and transfer from one focused drill to a full Interview Problem.
Recognize it when
Consider Heap Sort when the prompt's constraints and required operations match this shape: Build a heap and repeatedly extract extrema for in-place O(n log n) sorting.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Heap sort first turns the array into a max heap. Bottom-up heapify starts at the last internal node because leaves already satisfy the heap property.
The root is the maximum remaining value. Swap it with the last position of the active heap, which grows a sorted suffix that will never be touched again.
def heapify_trace(values):
values=values.copy();trace=[];n=len(values)
def sift(root,end):
while 2*root+1<end:
child=2*root+1
if child+1<end and values[child+1]>values[child]:child+=1
if values[root]>=values[child]:break
values[root],values[child]=values[child],values[root];root=child
for root in range(n//2-1,-1,-1):sift(root,n);trace.append(values.copy())
return traceImplement heapify_trace(values). Return array state after each bottom-up sift-down call.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
After each root swap, sift the new root down only within the shortened heap boundary. Children in the sorted suffix are no longer heap members.
def heap_sort(values):
values=values.copy();n=len(values)
def sift(root,end):
while 2*root+1<end:
child=2*root+1
if child+1<end and values[child+1]>values[child]:child+=1
if values[root]>=values[child]:break
values[root],values[child]=values[child],values[root];root=child
for root in range(n//2-1,-1,-1):sift(root,n)
for end in range(n-1,0,-1):values[0],values[end]=values[end],values[0];sift(0,end)
return valuesImplement heap_sort(values). Return an ascending sorted copy using an in-place max heap.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Heap sort guarantees O(n log n) with O(1) auxiliary array space but is not stable and has less cache-friendly access. Merge sort is stable and sequential but needs O(n) merge storage in standard arrays.
Say: “The active prefix is a max heap and the suffix is finalized ascending output. I move the root to the suffix and restore only the active heap.” State O(n) heapify plus O(n log n) extraction.
Python 3.11+
The code uses standard Python syntax and containers supported by the browser Judge. The Heap Sort proof does not depend on a minor Python release.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Heap Sort complete workflow | O(n log n) | O(n log n) | Heapify a copy, then repeatedly pop the minimum into the result. |
O(n) for the focused Sort with a Heap Frontier implementation.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Verify algorithm preconditions such as sorted input, nonnegative weights, acyclicity, or admissible heuristics before applying it.
Prevent it: State and verify this precondition before coding: The comparison, key domain, stability need, and memory constraints match the selected ordering method.
Avoid
def heap_sort(values):
passUse instead
def heap_sort(values):
import heapq
heap = list(values)
heapq.heapify(heap)
ordered = []
while heap:
ordered.append(heapq.heappop(heap))
return orderedWhere you will hit this: Sort with a Heap Frontier(opens in a new tab)
Returns the heap array directly even though heap order is not total order.
Prevent it: Preserve this proof obligation: The resolved prefix, suffix, rank, digit pass, or bucket remains in its documented final relation.
Avoid
def heap_sort(values):
import heapq
out=list(values); heapq.heapify(out); return outUse instead
def heap_sort(values):
import heapq
heap = list(values)
heapq.heapify(heap)
ordered = []
while heap:
ordered.append(heapq.heappop(heap))
return orderedWhere you will hit this: Sort with a Heap Frontier(opens in a new tab)
Include visited state, boundary cases, recursion depth, and hidden copying costs in correctness and complexity analysis.
Prevent it: Count every sort, slice, copy, membership check, heap update, and recursive frame before claiming O(n log n).
Avoid
def heap_sort(values):
import heapq
out=list(values); heapq.heapify(out); return outUse instead
def heap_sort(values):
import heapq
heap = list(values)
heapq.heapify(heap)
ordered = []
while heap:
ordered.append(heapq.heappop(heap))
return orderedWhere you will hit this: Car Fleet(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27