# Find Min and Max in one pass.

## Recommended Posts

```Func _MaxMin(ByRef \$aArray, ByRef \$vMin, ByRef \$vMax)
Local \$c, \$iSp
If Mod(UBound(\$aArray), 2) = 0 Then
\$iSp = 2
If \$aArray[0] < \$aArray[1] Then
\$vMin = \$aArray[0]
\$vMax = \$aArray[1]
Else
\$vMin = \$aArray[1]
\$vMax = \$aArray[0]
EndIf
Else
\$iSp = 1
\$vMin = \$aArray[0]
\$vMax = \$aArray[0]
EndIf
For \$c = \$iSp To UBound(\$aArray) - 1 Step 2
If \$aArray[\$c] < \$aArray[\$c + 1] Then
If \$aArray[\$c] < \$vMin Then
\$vMin = \$aArray[\$c]
EndIf
If \$aArray[\$c + 1] > \$vMax Then
\$vMax = \$aArray[\$c + 1]
EndIf
Else
If \$aArray[\$c + 1] < \$vMin Then
\$vMin = \$aArray[\$c + 1]
EndIf
If \$aArray[\$c] > \$vMax Then
\$vMax = \$aArray[\$c]
EndIf
EndIf
Next
EndFunc```

##### Share on other sites

Cool idea! One thinig though - I'd suggest you adjust your comparison routine to match that of the _ArraySort routines, as far the ability to compare INTs to Strings, etc.

## Create an account

Register a new account

×

• Wiki

• Back

• Git