471,355 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1404
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Eric Eggermann | last post: by
8 posts views Thread by Sueffel | last post: by
4 posts views Thread by dotNetDave | last post: by
43 posts views Thread by tshad | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.