code: public static int rank(int[] array, int elements, int target, int start,
                             int limit, int margin) {
  
          final int segments     = 256;
          int       elementCount = 0;
          int       currentLimit = limit;
  
          for (;;) {
              long interval = calcInterval(segments, start, currentLimit);
              int[] counts = countSegments(array, elements, segments, start,
                                           currentLimit);
  
              for (int i = 0; i < counts.length; i++) {
                  if (elementCount + counts[i] < target) {
                      elementCount += counts[i];
                      start        += interval;
                  } else {
                      break;
                  }
              }
  
              if (elementCount + margin >= target) {
                  return start;
              }
  
              if (interval <= 1) {
                  return start;
              }
  
              currentLimit = start + interval < limit ? (int) (start + interval)
                                                      : limit;
          }
      }
docs: With an unsorted int[] array and with target a positive integer in the
  range (1,array.length), finds the value in the range (start,limit) of the
  largest element (rank) where the count of all smaller elements in that
  range is less than or equals target. Parameter margin indicates the
  margin of error in target<p>
  
  In statistics, this can be used to calculate a median or quadrile value.
  A usage example applied to an array of age values is to determine
  the maximum age of a given number of people. With the example array
  given in countSegments, rank(array, c, 6000, 18, 65, 0) will return an age
  value between 18-64 (inclusive) and the count of all people aged between
  18 and the returned value(exclusive) will be less than or equal 6000.