473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ 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.Binar ySearch. 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 1493
binarysearch only works correctly if the items in the list are sorted.

"dotNetDave " <do********@dis cussions.micros oft.com> schrieb im Newsbeitrag
news:25******** *************** ***********@mic rosoft.com...
I have created my own comparer class using IComparer for use with
ArrayList.Binar ySearch. 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********@dis cussions.micros oft.com> wrote in message
news:25******** *************** ***********@mic rosoft.com...
I have created my own comparer class using IComparer for use with
ArrayList.Binar ySearch. 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********@dis cussions.micros oft.com> schrieb im Newsbeitrag
news:25******** *************** ***********@mic rosoft.com...
I have created my own comparer class using IComparer for use with
ArrayList.Binar ySearch. 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
3893
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 skipping some objects. The following code is supposed to start searching the InnerList from the beginning, and continue searching while there are...
8
3030
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 =...
2
1491
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 should be able to find the index of the element one higher than where I should insert. How? I'm not getting it.
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 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...
11
4999
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 the Compare-method as lean as possible, as it is summoned several 10 million times. in case of an integer the fastest way would be this i think:...
43
6971
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
3089
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 update another field with a 1. If I don't find a match I update it with a 0. I then remove that item from the arraylist and move on. The end result...
8
2582
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; m_ResultArray.Add ( aFoundObject);
2
2167
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 = racers.BinarySearch(delegate(Racer racer) { return racer.Car == "Ferrari"; });
0
7504
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...
0
7435
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7461
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...
0
7792
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...
0
6026
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...
1
5360
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
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...
0
3491
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...
1
1921
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 we have to send another system

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.