Re: Algorithms in C - Robert Sedgewick
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 09 Sep 2009 08:50:11 -0400
arnuld wrote:
On Wed, 09 Sep 2009 10:39:31 +0000, Richard Heathfield wrote:
In <ln7hw8zinh.fsf@xxxxxxxxxxxxxxx>, Keith Thompson wrote:
The average *successful* search
Er, quite so. I should have pointed that out. Thank you.
By that you mean the worst case successful search (20th comparison) or a successful search which is not worst case( e.g. 10th, 15th or even 3rd comparison) ??
Aha! Here's a perfect example of the kind of "Why
mathematics matters" issue you asked about earlier. No
search, successful or unsuccessful, will take more than
twenty comparisons. Some "lucky" successful searches
will take fewer, by happening to hit on the desired key
while the "active" interval is still relatively wide.
One particularly lucky successful search will find its
key on the very first comparison! How does the "luck
factor" skew the average? Is the effect significant, or
is it negligible? *That's* the kind of question that faces
a programmer often and often again. Care to have a go?
S
P
O
I
L
E
R
S
P
A
C
E
One way to tackle the problem is to set up one of those
recursions you seem to dread. Let's work "forward," following
the comparisons as they're made. The very first comparison in
an array of N elements lands on the desired key with probability
1/N. Otherwise (with probability 1-1/N) it misses, but leaves
you with only (N-1)/2 array locations still to be searched.
Letting C(N) be the average number of comparisons to find a
key in an array of size N (we're assuming a successful search),
we've got
C(N) = 1 * (1/N) + C((N-1)/2) * (1 - 1/N)
.... and if you can solve this recursion, you're all set. (The
decision of whether to start with C(1)=0 or C(1)=1 depends on
how sure you are that the key is actually present.)
By the way, the recursion I've shown is not quite right,
as it glosses over one crucial detail. Can you spot it? (Hint:
is N even or odd?)
Another way to attack the problem is to visualize the search
as a binary tree. The root node is the first array slot you
compare against. The left sub-tree is rooted at the slot you
check second if the original comparison comes out <, and the right
sub-tree starts at the second-compared slot if the first comparison
comes out >, and so on through the third, ..., twentieth comparison.
There are N nodes in the tree altogether. The number of comparisons
you make to arrive at any particular node K is one plus K's distance
from the root of the tree. So the average number of comparisons
is the tree's "internal path length," divided by N, plus one. If
you know something about the mathematics of trees, you are once
again all set.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.
- References:
- Algorithms in C - Robert Sedgewick
- From: arnuld
- Re: Algorithms in C - Robert Sedgewick
- From: Eric Sosman
- Re: Algorithms in C - Robert Sedgewick
- From: arnuld
- Re: Algorithms in C - Robert Sedgewick
- From: Richard Heathfield
- Re: Algorithms in C - Robert Sedgewick
- From: Keith Thompson
- Re: Algorithms in C - Robert Sedgewick
- From: Richard Heathfield
- Re: Algorithms in C - Robert Sedgewick
- From: arnuld
- Algorithms in C - Robert Sedgewick
- Prev by Date: Re: C coding guidelines
- Next by Date: Re: J'accuse
- Previous by thread: Re: Algorithms in C - Robert Sedgewick
- Next by thread: Re: Algorithms in C - Robert Sedgewick
- Index(es):
Relevant Pages
|