|
Binary Search Algorithm using VBScript |
|
|
|
|
Written by Sushma Charora
|
|
Wednesday, 30 August 2006 |
Binary Search Algorithm implemented in VBScript. Copy this script and use it for a multi-dimensional array.
Function BinarySearch(searchItem, startPos, endPos, TheArray, column_to_be_searched)
Dim midPoint
If startPos = endPos Then
If searchItem = TheArray(startPos, column_to_be_searched) Then
BinarySearch = startPos
Else
BinarySearch = -1 '-(startPos) Where it would have been...
End If
ElseIf startPos > endPos Then
BinarySearch = -1 '-(startPos)
Else
midPoint = CLng(startPos + ((endPos - startPos) / 2))
If searchItem = TheArray(midPoint, column_to_be_searched) Then
BinarySearch = midPoint
Else
If searchItem < TheArray(midPoint, column_to_be_searched) Then
BinarySearch = BinarySearch(searchItem, startPos, midPoint - 1, TheArray, column_to_be_searched)
Else
BinarySearch = BinarySearch(searchItem, midPoint + 1, endPos, TheArray, column_to_be_searched)
End If
End If
End If
End Function 'BinarySearch
|
|
Last Updated ( Tuesday, 06 November 2007 )
|