I'd like to make a minor tweak to my solution. There's a sixth parameter "size" for binary_search, which is the point at which it reverts to a linear search, and it's defaulted to 512. That means the small
@F array will be done with a linear search, after all.
I set the size down and did one more optimization, as follows:
my $lo_pos = binary_search(0,$#F,$lo,\&reader,\@F,2);
my $hi_pos = binary_search($lo_pos,$#F,$hi,\&reader,\@F,2);
I added a counter to the reader function and got the following output:
Did 6 probes for low
Did 6 probes for high
32,33,34,35
Did 6 probes for low
Did 7 probes for high
50,51,52,53,54,55,56,57,58,59,60,61,62,63,64
This is compared to a total of 78 probes the original way.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.