By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,672 Members | 1,644 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,672 IT Pros & Developers. It's quick & easy.

BinarySearch Not Searching Through All Items

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.