'Följande kod placerade jag i en modul (inte FORM)
Option Explicit
Private Declare Function GetTickCount& Lib "kernel32" ()
Private Const MINVALUE As Integer = 0
Private Const MAXVALUE As Integer = 9999
Sub main()
Dim sort As CSortArrays
Dim arrInts() As Integer
Dim arrInts2() As Integer
Dim i As Integer
Dim tickStart As Long
Dim tickEnd As Long
Randomize
Set sort = New CSortArrays
Debug.Print String(30, "=")
Debug.Print "Generera, blanda, och sortera " & CLng(MAXVALUE - MINVALUE + 1) & " tal."
Debug.Print "Initiate Array"
tickStart = GetTickCount
arrInts = GetRandomArray(MINVALUE, MAXVALUE)
tickEnd = GetTickCount
Debug.Print "Tid= " & CStr(tickEnd - tickStart) & " ms."
'Gör en kopia av den blandade arrayen för att kunna göra en jämförelse
'mellan sorteringsalgoritmerna på lika villkor.
arrInts2 = arrInts
Debug.Print "Sorterings algoritm: Quicksort"
tickStart = GetTickCount
sort.SortIntArray arrInts, SRTA_QUICKSORT
tickEnd = GetTickCount
Debug.Print "Tid= " & CStr(tickEnd - tickStart) & " ms."
Debug.Print "Sorterings algoritm: Selection sort"
tickStart = GetTickCount
sort.SortIntArray arrInts2, SRTA_SELECTIONSORT
tickEnd = GetTickCount
Debug.Print "Tid= " & CStr(tickEnd - tickStart) & " ms."
Debug.Print String(30, "=")
End Sub
Private Function GetRandomArray(Optional ByVal StartValue As Integer = 0, Optional ByVal EndValue As Integer = &H7FFF) As Integer()
Dim arrRet() As Integer
Dim arrSet() As Boolean
Dim RndNum As Integer, temp As Integer
Dim lngStart As Long, lngEnd As Long, i As Long
lngStart = StartValue: lngEnd = EndValue
If lngEnd - lngStart < 0 Then Exit Function
' ReDim arrRet(lngEnd - lngStart)
' ReDim arrSet(UBound(arrRet))
ReDim arrRet(lngEnd - lngStart)
ReDim arrSet(lngEnd - lngStart)
For i = LBound(arrRet) To UBound(arrRet)
RndNum = GetRandomNum(LBound(arrRet), UBound(arrRet))
If Not arrSet(i) Then
arrRet(i) = i + lngStart
arrSet(i) = True
End If
If Not arrSet(RndNum) Then
arrRet(RndNum) = RndNum + lngStart
arrSet(RndNum) = True
End If
temp = arrRet(i)
arrRet(i) = arrRet(RndNum)
arrRet(RndNum) = temp
Next
GetRandomArray = arrRet
End Function
Private Function GetRandomNum(ByVal MinVal As Integer, ByVal MaxVal As Integer) As Long
Dim temp As Currency
Dim result As Currency
'Temp variablen används för att ta hand om när MaxVal har maxvärde
temp = CLng(MaxVal) - CLng(MinVal) + 1
GetRandomNum = Int((temp) * Rnd + MinVal)
End Function