IntermediateAbout 20 min

    Heap Sort

    Read the full article on Substack

    7 min read.

    A comparison-based sorting algorithm that uses a Binary Heap data structure. It divides its input into a sorted and an unsorted region, and iteratively shrinks the unsorted region by extracting the largest element.

    Think of it this way: Think of a corporate hierarchy where the boss (largest number) is always at the top. To sort, you repeatedly 'retire' the boss and promote the next best candidate to the top position.

    3827433982100123456

    The heap part of the array (indices 0..6) drawn as a tree:

    38[0]27[1]43[2]3[3]9[4]82[5]10[6]

    Start: arrange the array as a max-heap, then repeatedly move the max to the back.

    1/40

    Check your understanding

    Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.

    Sort with a heap

    Sequence

    Order the steps of in-place heap sort.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Quiz

    20 questions
    Question 1 of 20
    Score: 0

    What is the "Heap Property" in a Max Heap?

    Done with this lesson?

    Sign in free to keep your progress, streak and quiz scores.

    Keep going