in reply to Re^4: Binary search algorithm.
in thread Binary search algorithm.
After $try is modified, the next instruction is next which goes to the top of the while loop where $try is assigned a new value. At no point is the value modified by ++$try or --$try used anywhere so you could just do:
while ( $lo <= $hi ) { my $try = int( ( $lo + $hi ) / 2 ); if ( $array[ $try ] lt $word ) { $lo = $try + 1; # without increment there will be # a dead loop in the 4th case, # see the note at the bottem } elsif ( $array[ $try ] gt $word ) { $hi = $try - 1; } else { return $try } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Binary search algorithm.
by ikegami (Patriarch) on Aug 13, 2011 at 16:52 UTC | |
by jwkrahn (Abbot) on Aug 14, 2011 at 18:30 UTC | |
by BrowserUk (Patriarch) on Aug 14, 2011 at 18:44 UTC | |
by ikegami (Patriarch) on Aug 14, 2011 at 21:06 UTC |