in reply to Re^4: Modified Binary Search
in thread 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.
"I'd rather go naked than blow up my ass"

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.