title: How to improve this Java Binary search method to find the best percentile for a given value?
body: <p>I have a sorted array of percentile values of home prices baes on X transactions:</p>
  
  <p><code>Double[] arr = {2418.0, 2535.0, 2652.0, 2808.0, 2808.0, 2808.0, 2808.0, 2808.0, 2808.0, 3657.0, 3816.0, 4144.0, 5429.0, 5429.0, 5429.0, 5429.0, 5429.0, 5518.0, 5518.0, 5518.0, 5518.0, 5518.0, 5607.0, 5607.0, 5607.0, 5607.0, 5607.0, 5607.0, 5696.0, 5696.0, 5696.0, 5696.0, 5696.0, 5785.0, 5785.0, 5785.0, 5785.0, 5785.0, 5874.0, 5874.0, 5874.0, 5874.0, 5874.0, 5874.0, 5963.0, 5963.0, 5963.0, 5963.0, 5963.0, 5963.0, 6052.0, 6052.0, 6052.0, 6052.0, 6052.0, 6052.0, 6141.0, 6141.0, 6141.0, 6141.0, 6141.0, 6141.0, 6230.0, 6230.0, 6230.0, 6230.0, 6230.0, 6319.0, 6319.0, 6319.0, 6319.0, 6319.0, 6408.0, 6408.0, 6408.0, 6497.0, 6497.0, 6497.0, 6586.0, 6586.0, 6645.4, 6675.0, 6764.0, 6853.0, 6942.0, 7120.0, 7337.3, 7924.2, 8244.5, 8564.0, 8840.0, 9062.2, 9285.9, 9492.1, 9717.5, 10013.2, 10668.4, 12034.5, 13386.0, 22868.0};</code></p>
  
  <p>So the 1st percentile of home prices is 2418 and the 100th percentile of home prices is 22868. As with percentiles, based on the input, some percentiles may hold the same values (as <code>6141</code>, <code>6408</code> and others in the above example). </p>
  
  <p>Now I'm writing a method, that given a home price (not necessarily in the original X transactions), it will find the best percentile it belongs to. I wrote this binary search code that seems to work OK, but I feel it can be improved:</p>
  
  <p>`</p>
  
  <pre><code>public static int findRelevantPercentile(Double [] arr, double searchFor){   
      int start = 0;
      int end  = arr.length - 1;
      int middle;
      do{
          middle = (start + end) / 2;
          if (arr[middle] &gt;= searchFor){
              end = middle;
          } else {
              start = middle;
          }
      }while(start + 1 &lt; end);
  
      if (searchFor &gt;= arr[end]){
          return arr.length;
      } else{
          return start + 1;
      }
  }
  </code></pre>
  
  <p>`</p>
  
  <p>If the value that we are looking for is below the 1st percentile it should also be the 1st percentile.
  If the value that we are looking for is above the 100th percentile it should also be the 100th percentile.</p>
  
  <p>BTW - I'm aware of Arrays.binarysearch(..) method.</p>