def heapsort(values):
    """Heapsorts a list of values in nondecreasing order."""
    length = len(values)

    def pick_child(parent):
        left = parent * 2 + 1
        if left >= length:
            return None

        right = left + 1
        if right == length or values[left] >= values[right]:
            return left

        return right

    def sift_down(parent):
        while True:
            child = pick_child(parent)
            if child is None or values[parent] >= values[child]:
                break

            values[parent], values[child] = values[child], values[parent]
            parent = child

    # Convert the list into a maxheap.
    for parent in range(length // 2, -1, -1):
        sift_down(parent)

    # Extract each element from the maxheap, placing them from right to left.
    while length > 1:
        length -= 1
        values[0], values[length] = values[length], values[0]
        sift_down(0)