PC Review


Reply
Thread Tools Rate Thread

BinarySearch Not Searching Through All Items

 
 
=?Utf-8?B?ZG90TmV0RGF2ZQ==?=
Guest
Posts: n/a
 
      22nd Feb 2005
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
 
Reply With Quote
 
 
 
 
cody
Guest
Posts: n/a
 
      22nd Feb 2005
binarysearch only works correctly if the items in the list are sorted.

"dotNetDave" <(E-Mail Removed)> schrieb im Newsbeitrag
news:253DAB99-7D07-4B02-91F4-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Peter van der Goes
Guest
Posts: n/a
 
      22nd Feb 2005

"dotNetDave" <(E-Mail Removed)> wrote in message
news:253DAB99-7D07-4B02-91F4-(E-Mail Removed)...
>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.


 
Reply With Quote
 
Mattias Sjögren
Guest
Posts: n/a
 
      22nd Feb 2005
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.
 
Reply With Quote
 
=?Utf-8?B?ZG90TmV0RGF2ZQ==?=
Guest
Posts: n/a
 
      22nd Feb 2005
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" <(E-Mail Removed)> schrieb im Newsbeitrag
> news:253DAB99-7D07-4B02-91F4-(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Searching for items in Outlook Ben Microsoft Outlook Discussion 4 7th May 2009 08:47 AM
MS Office: Sent Items Searching old not new James Microsoft Outlook Discussion 0 24th Jun 2008 02:15 PM
Searching for different items B_cooper123 Microsoft Excel Misc 1 23rd Apr 2008 04:29 PM
Searching for multiple items? =?Utf-8?B?R3JhbnQ=?= Windows Vista File Management 0 21st Jun 2006 02:25 AM
ASP 2.0: searching for items in other profiles Ya Ya Microsoft ASP .NET 0 2nd May 2006 10:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:47 AM.