473,612 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

BUG?! *** Array.BinarySea rch 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.BinarySea rch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine (index.ToString ) ' How no match???

index = Array.BinarySea rch(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 3939
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{d ot}com

"Mario" <mz******@DONTW ANTSPAMmail.pt> wrote in message news:uz******** ******@tk2msftn gp13.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.BinarySea rch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine (index.ToString ) ' How no match???

index = Array.BinarySea rch(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{d ot}com

"One Handed Man" <he******@hotma il.com> wrote in message news:c0******** **@hercules.bti nternet.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{d ot}com

"Mario" <mz******@DONTW ANTSPAMmail.pt> wrote in message news:uz******** ******@tk2msftn gp13.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.BinarySea rch(sexy, "BAba") ' index = -11 ?? BUG!!
Debug.WriteLine (index.ToString ) ' How no match???

index = Array.BinarySea rch(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
2514
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 doing a similar type of search, what is the most efficient method you have found for doing this? I can see doing this by comparing the first byte of the string to each consecutive byte of the file until it is found, and then checking for the rest of...
13
12304
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 below the decimal variable from the arraylist. Thanks ever so much in advance
2
1497
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 come to the last element, the BinarySearch method fails.It gives me a return value which is negative although the element is very much there. I am debugging through the code and I find nothing wrong with it.Note that i am using the first overloaded...
8
3033
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 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)
8
3951
by: Jan | last post by:
I've got the following array: FileIn(Counter) How can i search the array for example "window" TIA
11
2417
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 following. I noticed that if I set: int upper = strings.GetUpperBound(0); I never match the last element in the array (i.e. "iii")
6
2702
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
0
8171
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8568
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8253
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5536
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4047
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4110
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1699
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.