Heapsort

Overview

Heapsort uses a max-heap to build a heap from an array and repeatedly extract the max element, producing a sorted array when finished.

Property Value
Best Case O(n)
Worst Case O(nlgn)
Avg. Case O(nlgn)
Aux. Memory O(1)
Stable No
Adaptive Yes
Visualization heapsort.gif

Loop Invariant

Heapsort has loop invariant P given by

A[0:i-1] is a max-heap containing the i smallest elements of A. A[i:n-1] contains the n - i largest elements of A sorted.

We prove P maintains the requisite properties:

Powered by Forestry.md