Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heapsort - What is the difference/relation between Percolate-down/Shift-down and Heapify operation?

Tags:

c

heapsort

What is the difference between Percolate-down/Shift-down and Heapify operation?

This is my Shift-down function in C. And, I have already implemented Heapsort using this code.

void IterativeShiftDown(int i)
{   
    int leftOrRightChildIndex = EMPTY;
    int currentIndex = i;
    int currentElement = EMPTY;
    int childElement = EMPTY;

    while(HasAnyChild(currentIndex))
    {
        if(HasBothChild(currentIndex))
        {
            if(GetLeftChildElement(currentIndex) > GetRightChildElement(currentIndex))
            {
                leftOrRightChildIndex = GetLeftChildIndex(currentIndex);
            }
            else
            {
                leftOrRightChildIndex = GetRightChildIndex(currentIndex);
            }
        }
        else if(HasLeftChild(currentIndex))
        {
            leftOrRightChildIndex = GetLeftChildIndex(currentIndex);
        }

        currentElement = GetElement(currentIndex);
        childElement = GetElement(leftOrRightChildIndex);

        if(currentElement < childElement)
        {
            SwapElements(currentIndex, leftOrRightChildIndex);
        }

        currentIndex = leftOrRightChildIndex;
    }
}

Can I create a Heapify() - function using this code?

If yes, how?

Moreover, what is the algorithm to implement Heapsort using Heapify?

like image 474
user366312 Avatar asked Dec 12 '25 22:12

user366312


1 Answers

Shift down is typically an operation performed to insert an item into an existing heap.

heapify is typically an operation to create a heap from unordered data.

To expand on the function perreal supplied...

It starts in the middle because of the properties of a heap as implemented in an array. Each child element from index N in a heap is N*2 or N*2+1, so you know the last 1/2 of the elements have no children. Additionally, the children of a node in a heap have a relationship to the parent (consistently larger or smaller or whatever), but no relationship to each other - so the leaf children never need to be swapped.

So it starts at the middle bubbling/sifting each element down as far as needed.

There are times when heaps are great. Specifically, if you are receiving the input in a manner where you can do some CPU work for free while retrieving the data (async disk reads/db gets), you can build the heap incrementally almost for free.

Along those same lines, once you have the data, if you want to populate some UI or can feed back the results in some piecemeal fashion, you have the initial results immediately and with low cost for retrieving each element.

It's not the fastest to have sorted data in the world; but it (as far as I'm concerned) the best way to amortize the cost of the sort across the input=>output phases as well as the processing phase of an algorithm. This can frequently be useful.

like image 182
Joe Avatar answered Dec 14 '25 15:12

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!