Re: Search a list/array of objects for specified criteria?
- From: "Bill Butler" <qwerty@xxxxxxxx>
- Date: Sat, 10 Mar 2007 21:11:01 GMT
"Oliver Sturm" <oliver@xxxxxxxxxxxx> wrote in message
news:xn0f3jru11cjl6j009@xxxxxxxxxxxxxxxxxxxxxxx
Hello Rick,
In short, you recommend using the Find method of the List<T> class.
This performs a linear search, which is basically what I'm doing now.
It's acceptable but hardly optimal. I believe that a binary search
could be more efficient, even where the distribution is uneven.
Hm... I'm not sure really - it's a mathematical exercise to prove
whether it would be more efficient under the circumstances or not.
There are two problems:
* the distribution may be seriously uneven, we (I at least) don't
have any information on that
* the decision of whether or not a date falls in a given range is not
only dependent on one distributed value, but on two different ones.
Neither of these points will effect the speed of the search in the
slightest.
Remember, we are not talking about a (possibly imbalanced) binary tree.
We are talking about using a binary search algorithm over a sorted list.
That algorithm will give you the same performance as a perfectly
balanced tree. Uneven distributions of the data will not impact the
search in the slightest.
Also remember that Rick said:
<quote>
I have a large list of objects where each object has a unique
(non-overlapping) date range. The list is sorted chronologically
</quote>
Since the date ranges do NOT overlap there is a clear sequence of data
points
obj1.Start
obj1.End
obj2.Start
obj2.End
....
So, You perform a Binary search using either Start or end (the choice is
irrelevant). This seach will go as O(logN). If you were lucky enough to
hit the date on the money, then you are done. If you did not find the
exact date (far more likely), the algorithm will return a negative
number, which is the bitwise complement of the index of the next
element. In other words, the algorithm has in effect told you which item
in the Array you need to examine (off by one is more like it). Then it
is a simple matter of seeing if your date falls within the Start and End
dates for the object in question.
Of course, unless the number of data points is substantial, the total
time would be negligable in either case.
Hope this helps
Bill
<snip>
.
- Follow-Ups:
- Re: Search a list/array of objects for specified criteria?
- From: Oliver Sturm
- Re: Search a list/array of objects for specified criteria?
- References:
- Search a list/array of objects for specified criteria?
- From: Rick
- Re: Search a list/array of objects for specified criteria?
- From: Oliver Sturm
- Re: Search a list/array of objects for specified criteria?
- From: Rick
- Re: Search a list/array of objects for specified criteria?
- From: Oliver Sturm
- Search a list/array of objects for specified criteria?
- Prev by Date: Re: Paste a texte into the current window
- Next by Date: Re: Search a list/array of objects for specified criteria?
- Previous by thread: Re: Search a list/array of objects for specified criteria?
- Next by thread: Re: Search a list/array of objects for specified criteria?
- Index(es):
Relevant Pages
|
Loading