Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public class Bounds { int lower; int upper; public Bounds(int lower, int upper) { this.lower = lower; this.upper = upper; } } public Bounds BinarySearch(List&lt;int&gt; keys, double target) { // lower boundary case returns the smallest key as the lower and upper bounds if (target &lt; keys[0]) return new Bounds(0, 0); else if (target &lt; keys[1]) return new Bounds(0, 1); // upper boundary case returns the largest key as the lower and upper bounds else if (target &gt; keys[keys.Length - 1]) return new Bounds(keys.Length - 1, keys.Length - 1); else if (target &gt; keys[keys.Length - 2]) return new Bounds(keys.Length - 2, keys.Length - 1); else return BinarySearch(keys, target, 0, keys.Length - 1); } // 'keys' is a List storing all of the keys from your SortedList. public Bounds BinarySearch(List&lt;int&gt; keys, double target, int lower, int upper) { int middle = (upper + lower)/2; // target is equal to one of the keys if (keys[middle] == target) return new Bounds(middle - 1, middle + 1); else if (keys[middle] &lt; target &amp;&amp; keys[middle + 1] &gt; target) return new Bounds(middle, middle + 1); else if (keys[middle] &gt; target &amp;&amp; keys[middle - 1] &lt; target) return new Bounds(middle - 1, middle); if (list[middle] &lt; target) return BinarySearch(list, target, lower, upper/2 - 1); if (list[middle] &gt; target) return BinarySearch(list, target, upper/2 + 1, upper); } </code></pre> <p>This might work..I didn't test it out. If not, hopefully it's close enough that you can use it with minor tweaks. This is a strange problem, so I handled all of the boundary cases so I didn't have to think about what the algorithm would do when the range was down to 2 elements or less.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload