|
-
May 23rd, 2005, 04:25 AM
#4
New Member
Re: VB - Binary Search in array (array MUST be sorted)
So, related to the "bug" (I'm don't sure if it's one) i wrote about in http://vbforums.com/showpost.php?p=2019158&postcount=3 ,
here's an example about what I mean.
I am using the normal function ArrBinarySearch, but just with 2 lines added for debugging.
VB Code:
Private Function ArrBinarySearch(ByRef sArray() As String, _
ByRef sSearch As String, _
ByVal lFirst As Long, _
ByVal lLast As Long, _
Optional ByVal Compare As VbCompareMethod = vbBinaryCompare) As Long
Dim lMid As Long
Dim lStrC As Long
On Error Resume Next
If lFirst = lLast Then
ArrBinarySearch = lFirst
Else
lMid = (lFirst + lLast) \ 2 '<-- ###########
[b] MsgBox " "
Debug.Print "lFirst: " & lFirst & " lLast: " & lLast & " --> lMid: " & lMid[/b]
lStrC = StrComp(sSearch, sArray(lMid), Compare)
Select Case lStrC
Case -1
ArrBinarySearch = ArrBinarySearch(sArray(), sSearch, lFirst, lMid)
Case 0
ArrBinarySearch = lMid
Case 1
ArrBinarySearch = ArrBinarySearch(sArray(), sSearch, lMid, lLast)
End Select
End If
This one is now called by the function SearchArray, because the element i'll search for, isn't the first or the last one.
I'm calling SearchArray with
VB Code:
lResult = SearchArray(asMessageData(), sMessageData, vbTextCompare)
where asMessageData is the Array i've stored some Text of Mails and sMessageData is an new Mail, where i want to check if this one already exists.
This Array has an LBound of 1, and in this case, where it ends in an endless loop, the UBound of 4.
Bevore, this also happend when my UBound was 35, and all other Values after 4 chr(0).
Here's the Debug Output from the Line i've added above.
Code:
neu
neu
neu
lFirst: 1 lLast: 4 --> lMid: 2
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
lFirst: 1 lLast: 2 --> lMid: 1
"Neu" means, i've got a new mail, this includes that the search function has passed without any errors.
In the 4th case, as you see, it always returns to the value 1.
Don't know how to fix this
lg, freescale
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|