Templates and passing functions

M

Maistrye

Hi,

I'm not all that familiar with vba, and was wondering if there is
something similiar to:

1) C++ Templates
2) C/C++ Passing functions as parameters

I would like to have the equivalent of the qsort function from C/C++
(and I'd prefer not to have to specify Variant as the type, as I've
heard that is slower).

Thanks,
Scott
 
G

Guest

I recently had to write a qsort for an array of 100000 numbers.
Perhaps this will point you in the right direction:

Public Sub mySort()

Dim x As Long
Dim y As Long
Dim idx As Long
Dim Nums(0 To 99999) As Long

'load array
For y = 1 To 4
For x = 1 To 25000
idx = (((y - 1) * 25000) + x)
Nums(idx - 1) = CLng(Sheets("RECS2").Cells(x, y))
Next
Next

'sort array
Call quick(Nums(), 100000)

End Sub

Public Sub quick(myNums() As Long, count As Long)
Call qs(myNums(), 0, (count - 1))
End Sub

Public Sub qs(myNums() As Long, leftNum As Long, rightNum As Long)
Dim i As Long
Dim j As Long
Dim x As Long
Dim y As Long

i = leftNum
j = rightNum
x = myNums((leftNum + rightNum) / 2)

Do
Do While myNums(i) < x And i < rightNum
i = i + 1
Loop
Do While x < myNums(j) And j > leftNum
j = j - 1
Loop
If i <= j Then
y = myNums(i)
myNums(i) = myNums(j)
myNums(j) = y
i = i + 1
j = j - 1
End If
Loop While i <= j

If leftNum < j Then
Call qs(myNums(), leftNum, j)
End If
If i < rightNum Then
Call qs(myNums(), i, rightNum)
End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top