## Heapsort(): an in-place sorting algorithm:

- Maximum element is at A[1]
- Discard by swapping with element at A[n]
- Decrement heap_size[A]
- A[n] now contains correct value

- Restore heap property at A[1] by calling Heapify()
- Repeat, always swapping A[1] for A[heap_size(A)]

## Running time: O(n lg n)

- BuildHeap: O(n), Heapify: n * O(lg n)

Previous slide | Next slide | Back to first slide | View graphic version |