Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Heap Sort

Marc LorenzMarc Lorenz Member Posts: 25
So i found a vb6 code for it but can't quite understand it. I also have a C code for it and tried to use it in vb6(revised it for vb) but i can't get ti to work. Here is the code...
[code]
Private Sub Command1_Click()
ctr = arrsize

While (ctr > 0)

Heapsort (ctr)
ctr = ctr - 1

Wend

For ctr = 0 To 9

List1.AddItem (arr(ctr))

Next ctr
_______________________________________________________

Public Function Heapsort(upbound As Long) As Long

Dim mchild As Integer, lchild As Integer, rchild As Integer
Dim temp As Integer, ctr2 As Integer, root As Integer

root = (upbound - 1) / 2

ctr = root
While ctr >= 0
ctr2 = root
While ctr2 >= 0
lchild = (2 * ctr2) + 1
rchild = (2 * ctr2) + 2
If (lchild <= upbound) And (rchild <= upbound) Then
If (arr(rchild) >= arr(lchild)) Then
mchild = rchild
Else
mchild = lchild
End If
Else
If (rchild > upbound) Then
mchild = lchild
Else
mchild = rchild
End If
End If

If (arr(ctr2) < arr(mchild)) Then
temp = arr(ctr2)
arr(ctr2) = arr(mchild)
arr(mchild) = temp
End If
ctr2 = ctr2 - 1
Wend
ctr = ctr - 1
Wend

temp = arr(0)
arr(0) = arr(upbound)
arr(upbound) = temp

End Function

[/code]

Any help is appreciated. Thanks.
Sign In or Register to comment.