' ' File: AlsoArray.vbs (Based on BinarySearch) ' Author: Kelvin Sung ' ' ' ***************************************************************************************** Option Explicit Dim MyArray MyArray = Array(1, 4, 7, 10, 12, 17, 29, 49, 52, 67, 72, 78, 80, 81, 83, 92) 'index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ' ***************************************************************************************** ' BEGIN of the main script ' Dim Min, Max, findNumber, foundIndex Min = MyArray(0) Max = MyArray(UBound(MyArray)) MsgBox "Input Array Max=" & Max & " Min=" & Min ' ' If (WhatAnArray(MyArray, 91, foundIndex)) Then MsgBox "91: Found at index=" & foundIndex Else MsgBox "91: Not Found!" End If If (WhatAnArray(MyArray, 7, foundIndex)) Then MsgBox "7: Found at index=" & foundIndex Else MsgBox "7: Not Found!" End If Function WhatAnArray(ByRef anArray, ByVal aNumber, ByRef findIndex) Dim low, high WhatAnArray = False low = 0 high = UBound(anArray) ' initialize the variables ... Do While ( low <= high ) And ( WhatAnArray = False ) MsgBox "Low=" & low & " High=" & high findIndex = CInt( ( (low + high) / 2) ) If (CInt(aNumber) = anArray(findIndex)) Then WhatAnArray = True Else If (CInt(aNumber) > anArray(findIndex)) Then low = findIndex + 1 Else high = findIndex - 1 End If End If Loop End Function