473,503 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BUG?! *** Array.BinarySearch sucks!

Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:
Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?
Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :-)
Mario
Nov 20 '05 #1
2 3931
Simple, your array is not sorted. BAb is smaller than b because the sort order is character by character and B is smaller than b so should appear before it.

Regards - OHM

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message news:uz**************@tk2msftngp13.phx.gbl...
Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:
Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?
Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :-)
Mario
Nov 20 '05 #2
In fact to further illustrate this, if you use the sort method, look at your array after it is sorted, it sorts the array in descending order.

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

"One Handed Man" <he******@hotmail.com> wrote in message news:c0**********@hercules.btinternet.com...
Simple, your array is not sorted. BAb is smaller than b because the sort order is character by character and B is smaller than b so should appear before it.

Regards - OHM

--
Best Regards - OHM
one.handed.man{at}BTInternet{dot}com

"Mario" <mz******@DONTWANTSPAMmail.pt> wrote in message news:uz**************@tk2msftngp13.phx.gbl...
Hi there,

I'm writing a piece of code with VS.Net 2003, Framework 1.1.
And I can't make BinarySearch to work right. Look at this sample:
Dim sexy() As String = {"-", "a", "b", "ba", "A", "Aa", "Ab", _
"Aba", "B", "Ba", "Bb", "Bba", "BA", "BAa", "BAb", "BAba"}
Dim index As Integer

index = Array.BinarySearch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine(index.ToString) ' How no match???

index = Array.BinarySearch(sexy, "a") ' index = 1
Debug.WriteLine(index.ToString) ' this one works fine

' so, wtf I'm doing wrong?
Please help me if you can, because I'm bugged; and I dont want to go
with a silly For Each, If Then, snoopy loopy. Yea, I know I can also use
the Array.IndexOf, but expert writter Francesco Balena says it's slower
then BinarySearch on large arrays, that is my case.

Thank you very much!
Kind regards, :-)
Mario
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2500
by: Skwerl | last post by:
I'm trying to quickly grab the dimensions out of the headers of JPEG files. I need to look for the hex string FFC0001108 in a file that will be from 1-40 megs in size. If someone has experience...
13
12290
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
2
1483
by: Yogi21 | last post by:
Hi I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I...
8
3025
by: Sueffel | last post by:
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...
8
3940
by: Jan | last post by:
I've got the following array: FileIn(Counter) How can i search the array for example "window" TIA
11
2398
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
6
2693
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7274
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.