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

How to implement a BinarySearch on a custom Collection?

P: n/a
Lee

I have a custom collection that implements IComarer right now which
implements sorting. I've googled and it seems that built in binary
search is not available in the underlying List object.

If a list, collection, etc is sorted than I understand that I can
"half" it down right? Can anyone provide an example or resource where
I can look?

Thanks,

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
Mar 1 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Lee wrote:
I have a custom collection that implements IComarer right now which
implements sorting. I've googled and it seems that built in binary
search is not available in the underlying List object.

If a list, collection, etc is sorted than I understand that I can
"half" it down right? Can anyone provide an example or resource where
I can look?

Thanks,

You can use Reflector to take a look in System.Array.BinarySearch.
There you will find how Microsoft handles that.
Regards
Torsten
Mar 1 '06 #2

P: n/a
Hello, Lee!

L> If a list, collection, etc is sorted than I understand that I can
L> "half" it down right? Can anyone provide an example or resource where
L> I can look?

Is it what you need?
( http://www.brpreiss.com/books/opus6/html/page452.html )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 1 '06 #3

P: n/a
Lee
Vadym Stetsyak enlightened me by writing:
Hello, Lee!

Is it what you need?
( http://www.brpreiss.com/books/opus6/html/page452.html )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com


Thanks Vadym,

So it does need to be a recursive aglo then...

Thanks again,

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
Mar 1 '06 #4

P: n/a

"Lee" <lu*************@yahoo.com> wrote in message
news:Os***************@TK2MSFTNGP10.phx.gbl...
Vadym Stetsyak enlightened me by writing:
Hello, Lee!

Is it what you need?
( http://www.brpreiss.com/books/opus6/html/page452.html )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Thanks Vadym,

So it does need to be a recursive aglo then...


No - typically a binary search of linear storage does not have to be
recursive - although the max. # of recursions is of course log2(n), usually
there will be no problem with recursion depth.

Here's the Wikipedia version of both the recursive and non-recursive
versions. (Tail recursion elimination may render the recursive presentation
as non-recursive anyway, and it's slightly more pleasing looking...)
http://en.wikipedia.org/wiki/Binary_search
---------------------------------------------------------------------

The algorithm
The most common application of binary search is to find a specific value in
a sorted list. To cast this in the frame of the guessing game (see Example
below), realize that we are now guessing the index, or numbered place, of
the value in the list.

The search begins by examining the value in the center of the list; because
the values are sorted, it then knows whether the value occurs before or
after the center value, and searches through the correct half in the same
way. Here is simple pseudocode which determines the index of a given value
in a sorted list a between indices left and right:

function binarySearch(a, value, left, right)
if right < left
return not found
mid := floor((left+right)/2)
if a[mid] = value
return mid
if value < a[mid]
return binarySearch(a, value, left, mid-1)
else
return binarySearch(a, value, mid+1, right)
Because the calls are tail-recursive, this can be rewritten as a loop,
making the algorithm in-place:

function binarySearch(a, value, left, right)
while left ? right
mid := floor((left+right)/2)
if a[mid] = value
return mid
if value < a[mid]
right := mid-1
else
left := mid+1
return not found
hope this helps,
m

Thanks again,

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."

Mar 1 '06 #5

P: n/a
Lee
Mike enlightened me by writing:

"Lee" <lu*************@yahoo.com> wrote in message
news:Os***************@TK2MSFTNGP10.phx.gbl...
Vadym Stetsyak enlightened me by writing:
Hello, Lee!

Is it what you need?
( http://www.brpreiss.com/books/opus6/html/page452.html )

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Thanks Vadym,

So it does need to be a recursive aglo then...


No - typically a binary search of linear storage does not have to be
recursive - although the max. # of recursions is of course log2(n),
usually there will be no problem with recursion depth.

Here's the Wikipedia version of both the recursive and non-recursive
versions. (Tail recursion elimination may render the recursive
presentation as non-recursive anyway, and it's slightly more pleasing
looking...) http://en.wikipedia.org/wiki/Binary_search
---------------------------------------------------------------------

hope this helps,
m


Thanks Mike. Great article and snippet.

Helps a lot.

--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
Mar 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.