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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.