473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

BUG: ArrayList BinarySearch

I can' believe my own eye, but it happens...there is a bug in
ArrayList.BinarySearch!! It should be such a simple function......

Here is the detail. (I'm using C#, don't know if this is C#'s problem
or the whole framework's problem.

ArrayList test = new ArrayList();

test.Add(10);
test.Add(7);
test.Add(2);

test.BinarySearch(10);

the Search would return -4 instead of 0!! When you debug it, you see
in the variable test is normal:

test[0] = 10, test[1] = 7, test[2] = 2.

Who wrote this function..........

Homa Wong
Nov 15 '05 #1
4 4511
Homa,
In order for the BinarySearch to work, you need to have a Sorted list!

As by definition a binary search can only search sorted values!

For details see:
http://msdn.microsoft.com/library/de...earchTopic.asp

Which states "Uses a binary search algorithm to locate a specific element in
the sorted ArrayList". Notice the word sorted in there!

Seeing as your list is sorted in descending order, you will need to use the
overloaded BinarySearch function that accepts a Comparer object that does
descending comparisons, as the default comparison will do ascending
comparisons!

Hope this helps
Jay

"Homa" <ho******@yahoo.com> wrote in message
news:a9**************************@posting.google.c om...
I can' believe my own eye, but it happens...there is a bug in
ArrayList.BinarySearch!! It should be such a simple function......

Here is the detail. (I'm using C#, don't know if this is C#'s problem
or the whole framework's problem.

ArrayList test = new ArrayList();

test.Add(10);
test.Add(7);
test.Add(2);

test.BinarySearch(10);

the Search would return -4 instead of 0!! When you debug it, you see
in the variable test is normal:

test[0] = 10, test[1] = 7, test[2] = 2.

Who wrote this function..........

Homa Wong

Nov 15 '05 #2
I can' believe my own eye, but it happens...there is a bug in
ArrayList.BinarySearch!!


No there isn't. The list must be sorted (in ascending order), but
yours obviously isn't.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #3
Homa:

No, it's not bug. Binary Searches depend on a Sorted List, and in your
example, it's not sorted. As such, you'll get weird behavior and that's
well documented and not a bug. The whole concept of a Binary search is
built upon ordering, how else coudl you divide and conquer like it does?

Sort your list and it works fine.

http://msdn.microsoft.com/library/de...earchtopic.asp

The first line in the documentation makes this pretty clear
HTH,

Bill
"Homa" <ho******@yahoo.com> wrote in message
news:a9**************************@posting.google.c om...
I can' believe my own eye, but it happens...there is a bug in
ArrayList.BinarySearch!! It should be such a simple function......

Here is the detail. (I'm using C#, don't know if this is C#'s problem
or the whole framework's problem.

ArrayList test = new ArrayList();

test.Add(10);
test.Add(7);
test.Add(2);

test.BinarySearch(10);

the Search would return -4 instead of 0!! When you debug it, you see
in the variable test is normal:

test[0] = 10, test[1] = 7, test[2] = 2.

Who wrote this function..........

Homa Wong

Nov 15 '05 #4
I'm so sorry. I can't believe myself.....sigh...

Thankyou,

Homa Wong

"William Ryan" <do********@nospam.comcast.net> wrote in message news:<un**************@TK2MSFTNGP11.phx.gbl>...
Homa:

No, it's not bug. Binary Searches depend on a Sorted List, and in your
example, it's not sorted. As such, you'll get weird behavior and that's
well documented and not a bug. The whole concept of a Binary search is
built upon ordering, how else coudl you divide and conquer like it does?

Sort your list and it works fine.

http://msdn.microsoft.com/library/de...earchtopic.asp

The first line in the documentation makes this pretty clear
HTH,

Bill
"Homa" <ho******@yahoo.com> wrote in message
news:a9**************************@posting.google.c om...
I can' believe my own eye, but it happens...there is a bug in
ArrayList.BinarySearch!! It should be such a simple function......

Here is the detail. (I'm using C#, don't know if this is C#'s problem
or the whole framework's problem.

ArrayList test = new ArrayList();

test.Add(10);
test.Add(7);
test.Add(2);

test.BinarySearch(10);

the Search would return -4 instead of 0!! When you debug it, you see
in the variable test is normal:

test[0] = 10, test[1] = 7, test[2] = 2.

Who wrote this function..........

Homa Wong

Nov 15 '05 #5

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

Similar topics

4
by: Pete Z | last post by:
Does anyone know why this snippet of code continues to return x with a value of -1? Is there an issue with ArrayList.BinarySearch? ArrayList myAL = new ArrayList(); myAL .Add(1); myAL...
8
by: Sek | last post by:
Folks, I have an ArrayList of integers. I have sorted the list already. Now, i want to find the index of the first element that is greater than a given number. How to accomplish this in C#?
4
by: Mike Dole | last post by:
I have an arraylist like the one with the Guitar Class sample in Q316302. Dim MycolliCol as arraylist Private Sub FillArray() MyColliCol.Add(New Colli(1, "STUK", 0)) MyColliCol.Add(New...
9
by: Paul Nations | last post by:
I've got arraylists of simple classes bound to controls. I need to search through those arraylists to set the correct SelectedItem in the control. The code looks like: Public Class...
2
by: Henry Padilla | last post by:
I have a list of strings and I would like to insert them in order as they come up. I am trying to use ArrayList.BinarySearch which (theoretically) returns the negative bitwise compliment. And I...
10
by: JohnR | last post by:
I have an arraylist of string values. I would like to search the arraylist to find the index of a particular string and I would like the search to be case insensitive. dim al as new arraylist...
6
by: John Veldthuis | last post by:
I have an ArrayList set up with a set of class objects. The class has 3 strings in it and are as follows ProdID GSP Description Okay now problem with getting all these into the list and...
1
by: garyusenet | last post by:
My Array list contains a collection of InternetExplorer object. One of properties of this object is HWND. I'm trying to search my arraylist for the InternetExplorer object that has a certain...
8
by: Guy | last post by:
Is there a better way to search identical elements in a sorted array list than the following: iIndex = Array.BinarySearch( m_Array, 0, m_Array.Count, aSearchedObject ); aFoundObject= m_Array;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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,...

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.