ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Templates and passing functions (https://www.excelbanter.com/excel-programming/370844-templates-passing-functions.html)

Maistrye[_8_]

Templates and passing functions
 

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


--
Maistrye
------------------------------------------------------------------------
Maistrye's Profile: http://www.excelforum.com/member.php...o&userid=36078
View this thread: http://www.excelforum.com/showthread...hreadid=573264


Les

Templates and passing functions
 
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

--
Les Torchia-Wells


"Maistrye" wrote:


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


--
Maistrye
------------------------------------------------------------------------
Maistrye's Profile: http://www.excelforum.com/member.php...o&userid=36078
View this thread: http://www.excelforum.com/showthread...hreadid=573264




All times are GMT +1. The time now is 02:56 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com