473,480 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

BinarySearch Not Searching Through All Items

I have created my own comparer class using IComparer for use with
ArrayList.BinarySearch. My class seems to work with BinarySearch, but the
problem is that my ArrayList has three items in it and it seems that
BinarySearch only searches the first two items. So if I happen to be looking
for the last item in the ArrayList, BinarySearch returns that it's not there.

Any ideas?
David McCarter
=====================
David McCarter
www.vsdntips.com
Jul 21 '05 #1
4 1483
binarysearch only works correctly if the items in the list are sorted.

"dotNetDave" <do********@discussions.microsoft.com> schrieb im Newsbeitrag
news:25**********************************@microsof t.com...
I have created my own comparer class using IComparer for use with
ArrayList.BinarySearch. My class seems to work with BinarySearch, but the
problem is that my ArrayList has three items in it and it seems that
BinarySearch only searches the first two items. So if I happen to be looking for the last item in the ArrayList, BinarySearch returns that it's not there.
Any ideas?
David McCarter
=====================
David McCarter
www.vsdntips.com

Jul 21 '05 #2

"dotNetDave" <do********@discussions.microsoft.com> wrote in message
news:25**********************************@microsof t.com...
I have created my own comparer class using IComparer for use with
ArrayList.BinarySearch. My class seems to work with BinarySearch, but the
problem is that my ArrayList has three items in it and it seems that
BinarySearch only searches the first two items. So if I happen to be
looking
for the last item in the ArrayList, BinarySearch returns that it's not
there.

Any ideas?
David McCarter
=====================
David McCarter
www.vsdntips.com


How are the objects in your ArrayList ordered?
Please excuse me if I'm belaboring the obvious, but a binary search
algorithm will only work correctly if the collection to be searched has been
sorted. The symptoms you describe are typical of what happens if a binary
search algorithm is applied to unsorted data.
If you did not apply the ArrayList.Sort method before calling BinarySearch,
that could be your problem.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Jul 21 '05 #3
David,
My class seems to work with BinarySearch, but the
problem is that my ArrayList has three items in it and it seems that
BinarySearch only searches the first two items.
That's how binary search works, and is what makes it perform well.

So if I happen to be looking
for the last item in the ArrayList, BinarySearch returns that it's not there.


As long as the list is properly sorted and your comparer is returning
the correct results, it should work.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #4
Oh, I did not know it had to be sorted. Thanks!

David

"cody" wrote:
binarysearch only works correctly if the items in the list are sorted.

"dotNetDave" <do********@discussions.microsoft.com> schrieb im Newsbeitrag
news:25**********************************@microsof t.com...
I have created my own comparer class using IComparer for use with
ArrayList.BinarySearch. My class seems to work with BinarySearch, but the
problem is that my ArrayList has three items in it and it seems that
BinarySearch only searches the first two items. So if I happen to be

looking
for the last item in the ArrayList, BinarySearch returns that it's not

there.

Any ideas?
David McCarter
=====================
David McCarter
www.vsdntips.com


Jul 21 '05 #5

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

Similar topics

6
3888
by: Eric Eggermann | last post by:
I'm going batty trying to figure out why this is not working. I'm trying to find all objects within a class derived from CollectionBase which meet a certain criteria, but the search seems to be...
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...
2
1487
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...
4
255
by: dotNetDave | last post by:
I have created my own comparer class using IComparer for use with ArrayList.BinarySearch. My class seems to work with BinarySearch, but the problem is that my ArrayList has three items in it and it...
11
4993
by: solandre | last post by:
my motivation is as follows: i have to binarysearch several million times an long that is several million items big. this costs time, although i use Array.Binarysearch<long>. so i try to keep...
43
6943
by: tshad | last post by:
Which is better to use with an ArrayList: BinarySearch or Contains? The list is only going to have strings in it and it will be sorted. Thanks, Tom
3
3081
by: Justin | last post by:
Here's a quick rundown of what I'm doing. I'm filling an arraylist with data. Then I loop through a dataset and grab a field to perform a search on the arraylist. Every time I find a match I...
8
2578
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;...
2
2163
by: tshad | last post by:
I can do the following with Find: Racer theRacer2 = racers.Find(delegate(Racer racer) { return racer.Car == "Ferrari"; }); But I can't seem to do the same with BinarySearch, this: int iktr...
0
7055
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,...
0
7060
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
7106
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
6760
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
7022
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
4501
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...
0
3013
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...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
206
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.