Perl: the Markov chain saw PerlMonks

### Re^6: Modified Binary Search

by JavaFan (Canon)
 on Jan 14, 2010 at 13:03 UTC ( #817397=note: print w/replies, xml ) Need Help??

in reply to Re^5: Modified Binary Search

How about finding the smallest index that contains a value equal or greater than your target; or highest equal or less?
That doesn't change the complexity.
And how about you post a full sub save all of us trying to recreate your thought?
Too much work to type out all the details.
The proof is in the pudding!
Get any textbook about introductions to algorithms.

Replies are listed 'Best First'.
Re^7: Modified Binary Search
by BrowserUk (Pope) on Jan 14, 2010 at 13:06 UTC
That doesn't change the complexity.

Rubbish!

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The point: A well implemented binary search is O(logN) worst case, but averages O(logN - 1).

But, in order to accommodate duplicates, you are forced to recind the possibility of early discovery, which forces the average case to the worst case. Eg. The complexity changes!

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
In big O notation O(logN - 1) and O(log N) are equivalent. They denote the same complexity order.

Though, that does not mean that the two algorithms are equally efficient. Actually they are not: Re^3: Modified Binary Search.

A well implemented binary search is O(logN) worst case, but averages O(logN - 1).
That's wrong on multiple levels. First of all, your notation is sloppy. O(log N) and O(log N - 1) are equivalent classes. Any function that is in one class is also in the other.

I assume you mean that on average, a well implemented binary search only needs log N - 1 comparisons on average. But that's not true either. That's only true if you only search for elements that are present. Each unsuccessful search will take ceil(log N) or floor(log N) comparisons.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://817397]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2021-08-02 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My primary motivation for participating at PerlMonks is: (Choices in context)

Results (22 votes). Check out past polls.

Notices?