3 ms·
You can also build the heap from the bottom up and change fix_heap a little which altogether is faster and simpler (thanks wiki): fix_heap = (y, size) ->
by scotty79 15y ago
You can also build the heap from the bottom up and change fix_heap a little which altogether is faster and simpler (thanks wiki):
fix_heap = (y, size) ->
loop
y1 = 2*y+1
if y1 >= size then break
if y1 + 1 < size && VA.gt(y1 + 1, y1)
y1++
if VA.lt(y, y1)
VA.swap(y, y1)
y = y1
else
break
for x in [VA.length >> 1 ... -1] by -1
fix_heap(x, VA.length)
for x in [VA.length-1 ... 0] by -1
VA.swap(x, 0)
fix_heap(0, x)
- thedufer 15y agoI put this implementation in as a prewritten sort. Thanks!