0

Array heapification using siftdown - max(heap) this is the result of swaping 45 with 77, I am interested in next step, is it 37 swapping with 77 or 45 swapping with 67, considering that this situation was done by 45 swapping with 77 and I looked at level 1(level 0 is 37), do I need to go back down to fix situation with 45 and 67 or should continue raising up and then fix bottom numbers? which operation would be done first in computer implementation?

                            |37|  
            |77|                               |59|  
    |63|             |45|               |54|          |11|
|31|    |39|     |48|    |67|
Sergey K.
  • 24,894
  • 13
  • 106
  • 174

1 Answers1

0

The normal way to do it is here: http://en.wikipedia.org/wiki/Heapsort#Pseudocode.

Start at index N/2 (in the diagram, the space holding 45.) Do a siftDOWN. (From your description, it appears you did a siftUP.) If you started with this diagram, you would compare 45 to it's children (48,67) and swap 45 and 67. Then subtract one from the index (item= 63), and do a siftdown there. Continue until you get to the root. When you swap with a node which has its own children, you need to do a siftdown on that node too.

If you follow this sequence in the diagram, you can see that this pattern will check all the nodes in the tree.

AShelly
  • 34,686
  • 15
  • 91
  • 152