473,414 Members | 1,636 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to implement a BinarySearch on a custom Collection?

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
5 2970
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
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
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
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...
6
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...
3
by: Guthrie Phalanx | last post by:
HI all Bit of a newbie on interfaces, I hope this is not a silly question. I am stuck on a problem where I want to create a custom collection of objects, all of whom implement the same...
15
by: Chuck Bowling | last post by:
I'm having problems doing an efficient keyword search on a text file likely to be smaller than 100k. I have a keyword list of about 200 strings and I need to search the file and mark all of the...
4
by: Mike Dole | last post by:
I have an arraylist like the one with the Guitar Class sample in Q316302. Dim MycolliCol as arraylist Private Sub FillArray() MyColliCol.Add(New Colli(1, "STUK", 0)) MyColliCol.Add(New...
43
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
2
by: Steven Blair | last post by:
With the example below, would it be possible to perform a Sort, then a BinarySearch on the structure? I have achieved the same result using a <stringbut not with a custom type. The Sort must be...
8
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;...
6
by: Venkatesh Bhupathi | last post by:
Hi All, I am trying to inherit the Generic IList<Tinterface to form the typed collection of objects of type T. Following is my sample code, Public Class Record { public string name;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.