Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In Binary search I posted a binary search snippet. Having recently revisited the code I've rolled in the cmp suggestion zby made, handle empty lists and generally pimped the code.

This is the updated code. Note that undef is returned for an empty list.

use strict; use warnings; my @array = (1, 3, 5, 8, 10); print "\n", join ", ", @array, "\n"; print "Found 0 at " . BinSearch (0, \&cmpFunc, \@array) . "\n"; print "Found 1 at " . BinSearch (1, \&cmpFunc, \@array) . "\n"; print "Found 2 at " . BinSearch (2, \&cmpFunc, \@array) . "\n"; print "Found 5 at " . BinSearch (5, \&cmpFunc, \@array) . "\n"; print "Found 8 at " . BinSearch (8, \&cmpFunc, \@array) . "\n"; print "Found 10 at " . BinSearch (10, \&cmpFunc, \@array) . "\n"; print "Found 11 at " . BinSearch (11, \&cmpFunc, \@array) . "\n"; @array = (1, 3, 5, 8,); print "\n", join ", ", @array, "\n"; print "Found 0 at " . BinSearch (0, \&cmpFunc, \@array) . "\n"; print "Found 1 at " . BinSearch (1, \&cmpFunc, \@array) . "\n"; print "Found 2 at " . BinSearch (2, \&cmpFunc, \@array) . "\n"; print "Found 5 at " . BinSearch (5, \&cmpFunc, \@array) . "\n"; print "Found 8 at " . BinSearch (8, \&cmpFunc, \@array) . "\n"; print "Found 9 at " . BinSearch (9, \&cmpFunc, \@array) . "\n"; @array = (1,); print "\n", join ", ", @array, "\n"; print "Found 0 at " . BinSearch (0, \&cmpFunc, \@array) . "\n"; print "Found 1 at " . BinSearch (1, \&cmpFunc, \@array) . "\n"; print "Found 2 at " . BinSearch (2, \&cmpFunc, \@array) . "\n"; @array = (); print "\n", join ", ", @array, "\n"; print "Found 0 at " . BinSearch (0, \&cmpFunc, \@array) . "\n"; sub cmpFunc { $_[0] <=> $_[1]; } -------------- 8< -------------- 1, 3, 5, 8, 10, Use of uninitialized value in concatenation (.) or string at C:\Docume +nts and Settings\Peter.WINDOMAIN\My Documents\PerlMonks\junk\noname.p +l line 31. Found 0 at -0.5 Found 1 at 0 Found 2 at 0.5 Found 5 at 2 Found 8 at 3 Found 10 at 4 Found 11 at 4.5 1, 3, 5, 8, Found 0 at -0.5 Found 1 at 0 Found 2 at 0.5 Found 5 at 2 Found 8 at 3 Found 9 at 3.5 1, Found 0 at -0.5 Found 1 at 0 Found 2 at 0.5 Found 0 at
sub BinSearch { my ($target, $cmp, $array) = @_; my $posmin = 0; my $posmax = $#$array; return undef if ! @array; return -0.5 if $cmp->($array->[0], $target) > 0; return $#$array + 0.5 if $cmp->($array->[-1], $target) < 0; while (1) { my $mid = int (($posmin + $posmax) / 2); my $result = $cmp->($array->[$mid], $target); if ($result < 0) { $posmin = $posmax, next if $mid == $posmin && $posmax != $ +posmin; return $mid + 0.5 if $mid == $posmin; $posmin = $mid; } elsif ($result > 0) { $posmax = $posmin, next if $mid == $posmax && $posmax != $ +posmin; return $mid - 0.5 if $mid == $posmax; $posmax = $mid; } else { return $mid; } } }

In reply to Binary Search - revisited by GrandFather

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found