Skip to contentKEEP THE CRAFTTHE SANCTUARY

Heap Sort

Sort a list with heap sort: build a max-heap with sift_down, then move the largest value to the end again and again.

The problem

Implement heap_sort(values). Return a new list with the same values in ascending order, and leave values itself unchanged. values may be empty and may contain repeated or negative numbers. Do not call sorted, list.sort or the heapq module: build the heap yourself.

Starter code

def heap_sort(values):
    pass

Public examples

  1. values = [5, 1, 3, 1]gives[1, 1, 3, 5]
  2. values = [1, 2, 3]gives[1, 2, 3]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.