/**
   * Partitions the instances around a pivot. Used by quicksort and kthSmallestValue.
   *
   * @param insts The instances on which the tree is (or is to be) built.
   * @param index The master index array containing indices of the instances.
   * @param attidx The attribution/dimension based on which the instances should be partitioned.
   * @param l The begining index of the portion of master index array that should be partitioned.
   * @param r The end index of the portion of master index array that should be partitioned.
   * @return the index of the middle element
   */
  protected static int partition(Instances insts, int[] index, int attidx, int l, int r) {

    double pivot = insts.instance(index[(l + r) / 2]).value(attidx);
    int help;

    while (l < r) {
      while ((insts.instance(index[l]).value(attidx) < pivot) && (l < r)) {
        l++;
      }
      while ((insts.instance(index[r]).value(attidx) > pivot) && (l < r)) {
        r--;
      }
      if (l < r) {
        help = index[l];
        index[l] = index[r];
        index[r] = help;
        l++;
        r--;
      }
    }
    if ((l == r) && (insts.instance(index[r]).value(attidx) > pivot)) {
      r--;
    }

    return r;
  }
 /**
  * Predicts the class memberships for a given instance. If an instance is unclassified, the
  * returned array elements must be all zero.
  *
  * @param inst the instance to be classified
  * @return an array containing the estimated membership probabilities of the test instance in each
  *     class
  */
 @Override
 public double[] getVotesForInstance(Instance inst) {
   double[] ret;
   inst.setDataset(dataset);
   if (this.isInit == false) {
     ret = new double[dataset.numClasses()];
   } else {
     ret = learner.getVotesForInstance(inst);
   }
   return ret;
 }
  /**
   * Method to validate the sorting done by quickSort().
   *
   * @param insts The instances on which the tree is (or is to be) built.
   * @param indices The master index array containing indices of the instances.
   * @param attidx The dimension/attribute based on which the instances should be sorted.
   * @param start The start of the portion in master index array that needs to be sorted.
   * @param end The end of the portion in master index array that needs to be sorted.
   * @throws Exception If the indices of the instances are not in sorted order.
   */
  private static void checkSort(Instances insts, int[] indices, int attidx, int start, int end)
      throws Exception {
    for (int i = start + 1; i <= end; i++) {
      if (insts.instance(indices[i - 1]).value(attidx) > insts.instance(indices[i]).value(attidx)) {
        System.out.println("value[i-1]: " + insts.instance(indices[i - 1]).value(attidx));
        System.out.println("value[i]: " + insts.instance(indices[i]).value(attidx));
        System.out.println("indices[i-1]: " + indices[i - 1]);
        System.out.println("indices[i]: " + indices[i]);
        System.out.println("i: " + i);
        if (insts.instance(indices[i - 1]).value(attidx) > insts.instance(indices[i]).value(attidx))
          System.out.println("value[i-1] > value[i]");

        throw new Exception("Indices not sorted correctly.");
      } // end if
    }
  }