"be consistent" PerlMonks

Re^5: Modified Binary Search

by BrowserUk (Pope)
 on Jan 14, 2010 at 11:55 UTC ( #817388=note: print w/replies, xml ) Need Help??

in reply to Re^4: Modified Binary Search

You can easily find the smallest index containing your target value

How about finding the smallest index that contains a value equal or greater than your target; or highest equal or less?

And how about you post a full sub save all of us trying to recreate your thought?

It doesn't increase the run time complexity

The proof is in the pudding!

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.

Replies are listed 'Best First'.
Re^6: Modified Binary Search
by salva (Canon) on Jan 14, 2010 at 13:37 UTC
How about finding the smallest index that contains a value equal or greater than your target
```sub binary_search {
my (\$str, \$a) = @_;
my \$l = 0;
my \$h = @\$a;
while (\$l < \$h) {
my \$p = int ((\$l + \$h) / 2);
if (\$a->[\$p] lt \$str) {
\$l = \$p + 1;
}
else {
\$h = \$p;
}
}
\$l
}

update: minor changes to the sub.

Re^6: Modified Binary Search
by JavaFan (Canon) on Jan 14, 2010 at 13:03 UTC
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.
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.

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2021-06-20 00:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
What does the "s" stand for in "perls"? (Whence perls)

Results (93 votes). Check out past polls.

Notices?