More notes coming soon!
Naïve Heapsort is a variant of selection sort. It uses a heap-based priority queue to sort the items. To do this, insert all items into a max-heap and then remove them one by one. (It is also possible to use a min-heap instead.) The first such item removed is placed at the end of the array, the next item right before the end, and so forth until that last item deleted is placed in position 0 of the array. Each insertion and deletion takes time and there are insertions and deletions, resulting in a runtime. With some more work, we can show that heapsort is in the worst case. This naïve version of heapsort uses space for the priority queue.
When sorting an array, we can avoid the memory cost by treating the array itself as a heap. To do this, we first heapify the array using bottom-up heap construction (taking time). We then repeatedly delete the max item, swapping it with the last item in the heap. Over time, the heap shrinks from items to 0 items, and the sorted list grows from 0 items to items. The resulting version is also .