Hi Andrea,
Andrea said:
yes, but that's the worst case.
Yes, but in every second case (the probability that the ball breaks on
floor 1-50 is 0.5 when the probability is equally distributed) you need
a linear search from 1 to 49. And this needs in worst case 49 tries
(avg. 25).
My worst case is 20! And the average is 10. (If my calculations are
correct.) I think, this is hard to top.
That's really a tough question. We should assume a specific distribution
of the "breaking position" (probably uniform), and see on average which
solution is more efficient, and by how much. This is not an easy problem.
Even from a qualitative point of view, the binary search solution tends
to lose a lot in some specific positions, since if the ball breaks the
search has to be stopped, but tends to be pretty good in others because
of the nature of binary search. Which one is better is difficult to tell.
Maybe, because of the problem that if the ball breaks the binary search
can't continue, another solution might be a mix: instead of a fixed
amount, increase it, making it square every time. Something like
positions 4,8,16,32,64...
I dont agree: In binary search, you need to halve the *remaining*
interval in every step. You do the opposite.
Of course you are right that binary search is not the proper solution
for this problem - but this is no argument for your algorithm.
The more I think about it, the more I think that my idea is optimal:
Do some kind of "linear search" on 10,20,30,40,50,...,100 with the first
ball and then (if the first ball e.g. breaks on 40) a linear search on
31,32,33,...39 with the second ball.
That's O(sqrt(N)). I think you cannot get any better.
Note that the cost measure I used is the number of tries you drop a ball.
A better cost mesaure might be the number of stairs you have to go
(which should approximately be linear to the time you need). With this
new measure, a different algorithm should be better (I explained how to
"parallelize" my algorithm - but of course that is not optimal as well).
Ciao,
Ingo