System.Array.BinarySearch

  • Thread starter Thread starter Sueffel
  • Start date Start date
S

Sueffel

Okay, I can certinatly use a For..Next loop to find the specified info I
need, but I'm thinking the BinarySearch will be faster, if I can get it
working LOL

Anyhew, got an array, may have 5 or 5,000 elements, and I need to find the
string, say it's "Start here".

I've tried this:
Dim MyObj as Object = "Start Here"
Dim I as Integer = System.Array.BinarySearch(MyArr,MyObj)

and I get stuff like -1267 (Or size of array)

Frustraiting,
Sueffel
 
Sueffel said:
Okay, I can certinatly use a For..Next loop to find the specified
info I need, but I'm thinking the BinarySearch will be faster, if I
can get it working LOL

Anyhew, got an array, may have 5 or 5,000 elements, and I need to
find the string, say it's "Start here".

I've tried this:
Dim MyObj as Object = "Start Here"
Dim I as Integer = System.Array.BinarySearch(MyArr,MyObj)

and I get stuff like -1267 (Or size of array)

Where is the problem? Read the description of the return value in the docs
for BinarySearch.
 
Hi Sueffel,

Are you an old Cobol programmer who had to do with the magic of the binary
search in that.

Why should a binary Search be faster.

Did you already look at the array.indexof method?

Cor
 
To resond to Armin first:

My array length is 1236, the BinarySearch is returning -1231, and in this
case I know that the string I'm looking for is at element 11, so there's no
correlation. I have followed the example to the letter, and this is waht I
get. I'm getting frustraited with this...

And now to William:

Here's my Code, verbatum:

'Air Code'
System.Array.Sort(FST) 'FST is my array with 1236 Elements in it
Dim MySearchObj As Object = "MD5 message digest"
I= System.Array.BinarySearch(FST, MySrchObj)
MsgBox(I)
'*******'

The only thting I can figure, is the string in the file is:
"MD5 message digest filename
"

problem is, .NET is trimming the end, so, I need to do a search only using
part of the string. I can do this very easily with a For...Next loop, but
with 5,000 elements, we can see that this is probably not the most efficient
way of doing this.

Thanks again,
Sueffel
 
* "Sueffel said:
Okay, I can certinatly use a For..Next loop to find the specified info I
need, but I'm thinking the BinarySearch will be faster, if I can get it
working LOL

Anyhew, got an array, may have 5 or 5,000 elements, and I need to find the
string, say it's "Start here".

I've tried this:
Dim MyObj as Object = "Start Here"
Dim I as Integer = System.Array.BinarySearch(MyArr,MyObj)

and I get stuff like -1267 (Or size of array)

<msdn>
Return Value

The index of the specified value in the specified array, if value is
found.

-or-

A negative number, which is the bitwise complement of the index of the
first element that is larger than value, if value is not found and value
is less than one or more elements in array.

-or-

A negative number, which is the bitwise complement of (the index of the
last element + 1), if value is not found and value is greater than any
of the elements in array.
</msdn>
 
Cor said:
Hi Sueffel,

Are you an old Cobol programmer who had to do with the magic of the binary
search in that.

Why should a binary Search be faster.

Did you already look at the array.indexof method?

Cor

No, no cobol programmer, but, binary is faster than string or hex searching.
nature of computing. I'm going to look at indexOf though, that may be my
answer.

Thanks again everyone,
Sueffel
 
Sueffel,
You do realize that BinarySearch does an exact match:
Dim MySearchObj As Object = "MD5 message digest"

Does element 11 have "MD5 message digest" exactly, if it does its index
should be returned.

Or does it have "MD5 message digest
filename", in which case it does not match, and you get the negative number.

As both the extra spaces and filename will not allow it to match!

In your For Next, I take it you are using String.StartsWith to get an
approximate match?

Hope this helps
Jay
 
Back
Top