Sorting algorithms/Heapsort: Difference between revisions

Content added Content deleted
(→‎{{header|FreeBASIC}}: fix more bugs)
Line 4,003: Line 4,003:
Dim temp As Integer
Dim temp As Integer


While root * 2 + 1 < eend
While root * 2 + 1 <= eend
Dim child As Long : child = root * 2 + 1
Dim child As Long : child = root * 2 + 1
If (child + 1 < eend) And (list(lb + child) < list(lb + child + 1)) Then
If (child + 1 <= eend) Then
child = child + 1
If (list(lb + child) < list(lb + child + 1)) Then
child = child + 1
End If
End If
End If
If list(lb + root) < list(lb + child) Then
If list(lb + root) < list(lb + child) Then
Line 4,023: Line 4,025:
Dim lb As Long : lb = LBound(list)
Dim lb As Long : lb = LBound(list)
Dim count As Long : count = UBound(list) - lb + 1
Dim count As Long : count = UBound(list) - lb + 1
Dim start As Long : start = lb + (count - 2) \ 2
Dim start As Long : start = (count - 2) \ 2
Dim eend As Long : eend = count - 1


While start >= 0
While start >= 0
SiftDown list(), start, count
SiftDown list(), start, eend
start = start - 1
start = start - 1
Wend
Wend

Dim eend As Long : eend = count - 1
Dim temp As Integer
Dim temp As Integer


Line 4,038: Line 4,040:
list(lb) = temp
list(lb) = temp


SiftDown list(), 0, eend
eend = eend - 1
eend = eend - 1

SiftDown list(), 0, eend
Wend
Wend
End Sub</lang>
End Sub</lang>