I need to code a special form of binary search. In this one, the array contents aren't known yet. To get a value for the array means running a process that takes 1 minute to complete. The time is spent creating the values, not in the search. I want to skip as many values as possible, only running the ones that fall into the window.

The special part comes in because there is an upper limit, and a lower limit. If the value is above the upper limit, I know that all values higher than this don't need to be calculated, they can all be set to MAX. Any value below the lower limit will be set to MIN, and all values less will be set to MIN. No need to get the actual value.

Given an array of 100 elements, what is the fastest way to find the window of values to process, while skipping the ones we don't need to process? The result is a sorted array, with MIN values up to the start of the window, and MAX values after the end of the window. How to find the minimum set of values to process that are in the window?

I doubt there is any Perl module to do this.

Basic algorithm would be something like:

Start at the middle. Run the process on the middle element. If the value is below the minimum, all the array up to that value can be set to MIN, no process needs to be run for those elements. If the value is above the maximum, all the array after that can be set to MAX, no process needs to be run for those elements. If the value is between minimum and maximum, then store the value in the array.

Repeat this until you find all the values between the minimum and maximum, and try to run as few processes as possible.

If the min and max are 1 and 10, for example, and the final array looks like 0,0,0,...1,2,3,4,5,10,101,102,103... How do I process only the 1,2,3,4,5,10 entries and skip as many of the others as possible?

The runtime savings is the goal. There are 100 elements per array, and 12 arrays per set, and 5,000 sets.

any help appreciated, thanks, Bill P


In reply to Special binary search by olepi

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.