I'm implementing heapsort using a heap. To do this each value to be sorted is inserted. The insertion method calls heapifyUp()
(aka siftUp) so this means each time another value is inserted heapifyUp is called. Is this the most efficient way?
Another idea would be to insert all elements, and then call heapifyUp. I guess heapifyUp would have to be called on each one? Is doing it like this better?
Inserting each element will build the heap in O(n log n) time. Same thing if you add all the elements to an array and then repeatedly call
heapifyUp()
.Floyd's Algorithm builds the heap bottom-up in O(n) time. The idea is that you take an array that's in any order and, starting in the middle, sift each item down to its proper place. The algorithm is:
You start in the middle because the last length/2 items in the array are leaves. They can't be sifted down. By working your way from the middle, up, you reduce the number of items that have to be moved.
Example of the difference
The example below, turning an array of 7 items into a heap, shows the difference in the amount of work done.
The heapifyUp() method
Start at the end and bubble items up.
Move 4 to the proper place
Move 3 to its place
Move 2 to its place
Move 1 to its place
The heap is now in order. It took 8 swaps, and you still have to check 4, 2, and 1.
Floyd's algorithm
Start at the halfway point and sift down. In a 0-based array of 7 items, the halfway point is 3.
Move 1 to its place
Move 6 to its place
Move 5 to its place
Move 7 to its place
And we're done. It took 5 swaps and there's nothing else to check.