Re: ArrayList BinarySearch vs Contains
- From: "Dave Sexton" <dave@jwa[remove.this]online.com>
- Date: Sat, 11 Nov 2006 15:35:04 -0500
Hi Bill,
It seems you are suggesting that the scale of the data can affect whether a
required sorting operation will actually reduce the overall performance of an
algorithm so that it becomes worse than a linear search. Can this be true
even when the data is sorted as it's being entered into the structure?
Since I'm not one for micro-optimization either I'd rather just choose an
algorithm with an average performance of O(lg(n)) if I'm sure that there may
be substantial amounts of data at times and just swallow the performance hit
on those smaller collections. I guess if there is a diametric aspect to the
data then maybe switching between linear and binary algorithms at runtime
might be acceptable. I've never had a project that required this much thought
on search algorithms, however :)
Thanks.
--
Dave Sexton
"Bill Butler" <qwerty@xxxxxxxx> wrote in message
news:_4q5h.358$5P2.166@xxxxxxxxxxx
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:12la6mse5o0gsf6@xxxxxxxxxxxxxxxxxxxxx
<snip>
If you have a sorted array, it's true that the worst case is O(log(n)).
Everyone is making wonderful points on this topic, but I wanted to underline
a few key points.
As Peter said, a BinarySearch on a sorted ArrayList (or a balanced Binary
tree) will have a Worst case performance of O(log(n)).
Another important point of Big O notation is that it describes Asymptotic
behavior. Thus for sufficiently large n an O(log(n)) algorithm will
outperform an O(n) algorithm. The problem is determining what constitutes
"sufficiently large n". For small values of n, O(log(n)) algorithms often
perform WORSE than linear algorithms do to the additional setup complexity.
Of course, for small collections, ANY search will be fast, so it doesn't
really matter.
Bill
.
- Follow-Ups:
- Re: ArrayList BinarySearch vs Contains
- From: Bill Butler
- Re: ArrayList BinarySearch vs Contains
- From: Brian Gideon
- Re: ArrayList BinarySearch vs Contains
- From: Peter Duniho
- Re: ArrayList BinarySearch vs Contains
- References:
- ArrayList BinarySearch vs Contains
- From: tshad
- Re: ArrayList BinarySearch vs Contains
- From: Carl Daniel [VC++ MVP]
- Re: ArrayList BinarySearch vs Contains
- From: tshad
- Re: ArrayList BinarySearch vs Contains
- From: Dave Sexton
- Re: ArrayList BinarySearch vs Contains
- From: Peter Duniho
- Re: ArrayList BinarySearch vs Contains
- From: Bill Butler
- ArrayList BinarySearch vs Contains
- Prev by Date: Re: C# using classes from C++ DLL
- Next by Date: Re: C# using classes from C++ DLL
- Previous by thread: Re: ArrayList BinarySearch vs Contains
- Next by thread: Re: ArrayList BinarySearch vs Contains
- Index(es):
Relevant Pages
|