Sorting algorithms/Heapsort: Difference between revisions

Content added Content deleted
(Added VBA)
(→‎{{header|FreeBASIC}}: fix more bugs)
Line 1,553: Line 1,553:
Dim As Long lb = LBound(hs)
Dim As Long lb = LBound(hs)


While root * 2 + 1 < end_
While root * 2 + 1 <= end_
Dim As ULong child = root * 2 + 1
Dim As ULong child = root * 2 + 1
If (child + 1 < end_) And (hs(lb + child) < hs(lb + child + 1)) Then
If (child + 1 <= end_) AndAlso (hs(lb + child) < hs(lb + child + 1)) Then
child = child + 1
child = child + 1
End If
End If
Line 1,570: Line 1,570:
Dim As Long lb = LBound(hs)
Dim As Long lb = LBound(hs)
Dim As ULong count = UBound(hs) - lb + 1
Dim As ULong count = UBound(hs) - lb + 1
Dim As Long start = lb + (count - 2) \ 2
Dim As Long start = (count - 2) \ 2
Dim As ULong end_ = count - 1


While start >= 0
While start >= 0
siftdown(hs(), start, count)
siftdown(hs(), start, end_)
start = start - 1
start = start - 1
Wend
Wend


Dim As ULong end_ = count - 1
While end_ > 0
While end_ > 0
Swap hs(lb + end_), hs(lb)
Swap hs(lb + end_), hs(lb)
siftdown(hs(), 0, end_)
end_ = end_ - 1
end_ = end_ - 1
siftdown(hs(), 0, end_)
Wend
Wend
End Sub
End Sub