www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Why does buildHeap sift both down and up?

I was looking through the heap primitives in std.algorithm.sorting and 
was surprised that buildHeap() at 
uses percolate(). In turn, percolate() has two stages, one that sifts 
up, the other that sifts down.

In fact all you need to do is sift down and stop as soon as the heap 
property is preserved. I created 
https://github.com/D-Programming-Language/phobos/pull/3933 which as far 
as I understand is correct and also faster.

Could experts please illuminate me?


Jan 15 2016