static private int[] worstIndices(double[] x,double propKept){
  if (propKept == 0)   return null;
  int n=x.length;
  int numNeg=0;
  for (  double aX1 : x)   if (aX1 < 0.0)   numNeg++;
  if (numNeg == 0)   return null;
  double[] xcopy=new double[numNeg];
  int j=0;
  for (  double aX : x)   if (aX < 0.0)   xcopy[j++]=aX;
  Arrays.sort(xcopy);
  int nkept=(int)Math.ceil(propKept * numNeg);
  double cutoff=xcopy[nkept - 1];
  int[] result=new int[nkept];
  int front=0, back=nkept - 1;
  for (int i=0; i < n; i++) {
    if (x[i] < cutoff)     result[front++]=i;
 else     if (x[i] == cutoff) {
      if (back >= front)       result[back--]=i;
    }
  }
  return result;
}

static private int [ ] worstIndices ( double [ ] x , double propKept ) { if ( propKept == 0 ) return null ; int n = x . length ; int numNeg = 0 ; for ( double aX1 : x ) if ( aX1 < 0.0 ) numNeg ++ ; if ( numNeg == 0 ) return null ; double [ ] xcopy = new double [ numNeg ] ; int j = 0 ; for ( double aX : x ) if ( aX < 0.0 ) xcopy [ j ++ ] = aX ; Arrays . sort ( xcopy ) ; int nkept = ( int ) Math . ceil ( propKept * numNeg ) ; double cutoff = xcopy [ nkept - 1 ] ; int [ ] result = new int [ nkept ] ; int front = 0 , back = nkept - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( x [ i ] < cutoff ) result [ front ++ ] = i ; else if ( x [ i ] == cutoff ) { if ( back >= front ) result [ back -- ] = i ; } } return result ; }
returns the array indices for the smallest propkept proportion of negative values in x . in the case of ties , priority is given to the earliest entries . size of resulting array will be propkept * ( number of negative entries ) rounded up .
