sampleArray(0)
sampleArray(ArrayLen(sampleArray()) - 1)
I don't think anything else would be faster than that. Internally it seems it uses the QuickSort algorithm which is probably the fastest in this situation.maybe it's not the fastest solution
I don't think anything else would be faster than that.
Sub Globals
Public arr(100000) As int64
Public bDoTiming
Public lTicks(1) As Int64
End Sub
Sub App_Start
Dim i
Dim lMin
Dim lMax
dzHW.New1
ArraysEx.New1
'fill the array
'--------------
For i = 0 To 99999
arr(i) = Rnd (100, 1000)
Next i
lMin = arr(0)
'first get min and max without sorting
'-------------------------------------
StartTimer
For i = 0 To 99999
If arr(i) > lMax Then
lMax = arr(i)
Else
If arr(i) < lMin Then
lMin = arr(i)
End If
End If
Next i
StopTimer("get min and max without sorting")
Msgbox("minimum: " & lMin, "maximum: " & lMax)
'then get min and max with sorting
'---------------------------------
StartTimer
ArraysEx.Sort(arr(),0, 100000, cNumber)
StopTimer("get min and max with sorting")
Msgbox("minimum: " & arr(0), "maximum: " & arr(99999))
End Sub
Sub StartTimer
lTicks(0) = dzHW.GetTickCount
End Sub
Sub StopTimer(strProcedure)
If Msgbox("Time taken in " & strProcedure & CRLF & CRLF & _
dzHW.GetTickCount - lTicks(0) & " milli-seconds", _
"Code timer", _
cMsgboxOKCancel, cMsgboxNone) = cCancel Then
bDoTiming = False
End If
End Sub
Sub App_Start
Dim i
Dim lMin
Dim lMax
Dim lMinIndex
Dim lMaxIndex
dzHW.New1
ArraysEx.New1
'fill the array
'--------------
For i = 0 To 99999
arr(i) = Rnd (100, 1000)
Next i
lMin = arr(0)
'first get min and max without sorting
'-------------------------------------
StartTimer
For i = 0 To 99999
If arr(i) > lMax Then
lMax = arr(i)
lMaxIndex = i
Else
If arr(i) < lMin Then
lMin = arr(i)
lMinIndex = i
End If
End If
Next i
StopTimer("get min and max without sorting")
Msgbox("minimum: " & lMin & " at index: " & lMinIndex, _
"maximum: " & lMax & " at index: " & lMaxIndex)
End Sub