Performs sorting based on the double values natural comparator.
{@link Double#NaN} values will  not be handled appropriately.

@param x the array to sort
@param start the starting index (inclusive) to sort
@param end the ending index (exclusive) to sort
175053
public static void sort(double[] x, int start, int end)
    {
        int a = start;
        int n = end-start;
        if (n < 7)/* Insertion sort on smallest arrays */
        {
            insertionSort(x, a, end);
            return;
        }
        
        int pm = a + (n/2);  /* Small arrays, middle element */
        if (n > 7)
        {
            int pl = a;
            int pn = a + n - 1;
            if (n > 40) /* Big arrays, pseudomedian of 9 */
            {        
                int s = n / 8;
                pl = med3(x, pl, pl + s, pl + 2 * s);
                pm = med3(x, pm - s, pm, pm + s);
                pn = med3(x, pn - 2 * s, pn - s, pn);
            }
            pm = med3(x, pl, pm, pn); /* Mid-size, med of 3 */
        }
        double pivotValue = x[pm];

        int pa = a, pb = pa, pc = end - 1, pd = pc;
        while (true)
        {
            while (pb <= pc && x[pb] <= pivotValue)
            {
                if (x[pb] == pivotValue)
                    swap(x, pa++, pb);
                pb++;
            }
            while (pc >= pb && x[pc] >= pivotValue)
            {
                if (x[pc] == pivotValue)
                    swap(x, pc, pd--);
                pc--;
            }
            if (pb > pc)
                break;
            swap(x, pb++, pc--);
        }

        
        int s;
        int pn = end;
        s = Math.min(pa - a, pb - pa);
        vecswap(x, a, pb - s, s);
        s = Math.min(pd - pc, pn - pd - 1);
        vecswap(x, pb, pn - s, s);

        //recurse 
        if ((s = pb - pa) > 1)
            sort(x, a, a+s);
        if ((s = pd - pc) > 1)
            sort(x, pn - s, pn);
        
    }
47207
public static INDArrayIndex interval(int begin, int stride, int end, boolean inclusive) {
        Preconditions.checkArgument(begin <= end, "Beginning index (%s) in range must be less than or equal to end (%s)", begin, end);
        INDArrayIndex index = new IntervalIndex(inclusive, stride);
        index.init(begin, end);
        return index;
    }
922006
static public void xA(double[] result, double[] x, double[][] A) {
    for (int i = 0; i < result.length; i++) {
      result[i] = 0.0;
      for (int j = 0; j < result.length; j++) {
        result[i] += (x[j] * A[j][i]);
      }
    }
  }
1289063
public void add(double x) {
        Cell[] as; long b, v; int[] hc; Cell a; int n;
        if ((as = cells) != null ||
                !casBase(b = base,
                        Double.doubleToRawLongBits
                                (Double.longBitsToDouble(b) + x))) {
            boolean uncontended = true;
            if ((hc = threadHashCode.get()) == null ||
                    as == null || (n = as.length) < 1 ||
                    (a = as[(n - 1) & hc[0]]) == null ||
                    !(uncontended = a.cas(v = a.value,
                            Double.doubleToRawLongBits
                                    (Double.longBitsToDouble(v) + x))))
                retryUpdate(Double.doubleToRawLongBits(x), hc, uncontended);
        }
    }
792308
private double[][] _start() {
    double[][] startState = new double[2][];
    startState[0] = new double[initialState.length];
    startState[1] = Arrays.copyOf(initialState, initialState.length);
    return startState;
  }
1223581
public static void sortMenu(BaseComponent parent, int startIndex, int endIndex) {
        List<BaseComponent> items = parent.getChildren();
        int bottom = startIndex + 1;

        for (int i = startIndex; i < endIndex;) {
            BaseComponent item1 = items.get(i++);
            BaseComponent item2 = items.get(i);

            if (item1 instanceof BaseMenuComponent && item2 instanceof BaseMenuComponent && ((BaseMenuComponent) item1)
                    .getLabel().compareToIgnoreCase(((BaseMenuComponent) item2).getLabel()) > 0) {
                parent.swapChildren(i - 1, i);

                if (i > bottom) {
                    i -= 2;
                }
            }
        }
    }
824711
private static void insertionSort(double[] keys, int[] vals, final int start, final int end) {
    // Classic insertion sort.
    for(int i = start + 1; i < end; i++) {
      for(int j = i; j > start; j--) {
        if(keys[j] >= keys[j - 1]) {
          break;
        }
        swap(keys, vals, j, j - 1);
      }
    }
  }
192306
public static void sort(int[] intArray) {
        int index = 0;
        int value = 0;
        
        for(int i = 1; i < intArray.length; i++) {
            index = i;
            value = intArray[index];
            

            while(index > 0 && value < intArray[index - 1]) {
                intArray[index] = intArray[index - 1];
                
                index--;
            }
            
            intArray[index] = value;
        }
    }
977071
public static INDArrayIndex[] createFromStartAndEnd(INDArray start, INDArray end) {
        if (start.length() != end.length())
            throw new IllegalArgumentException("Start length must be equal to end length");
        else {
            if (start.length() > Integer.MAX_VALUE)
                throw new ND4JIllegalStateException("Can't proceed with INDArray with length > Integer.MAX_VALUE");

            INDArrayIndex[] indexes = new INDArrayIndex[(int) start.length()];
            for (int i = 0; i < indexes.length; i++) {
                indexes[i] = NDArrayIndex.interval(start.getInt(i), end.getInt(i));
            }
            return indexes;
        }
    }
562585
public static void sortReverse(double[] keys, int[] values, int start, int end) {
    quickSortReverse(keys, values, start, end);
  }
934144
public static <T extends Comparable<? super T>> 
    void insertionSort( T[] arr, int start, int end )
    {
        int i;
        for ( int j = start + 1; j <= end; j++ ) {
            T tmp = arr[j];
            for ( i = j; i > start && tmp.compareTo( arr[i - 1] ) < 0; i-- ) {
                arr[ i ] = arr[ i - 1 ];
            }
            if ( i < j ) arr[ i ] = tmp;
        }
    }
1320096
public void parallelSort(double[] arrayToBeSorted,
            double[] arrayThatFollowsTheSort) throws Exception {
        int pow = 1, div = 1;
        int h[];

        for (int i = 1; i * i <= arrayToBeSorted.length; i++)
            if (arrayToBeSorted.length % i == 0)
                div = i;
        Rows = div;
        Cols = arrayToBeSorted.length / div;
        for (Log = 0; pow <= Rows; Log++)
            pow = pow * 2;

        h = new int[Rows];
        for (int i = 0; i < Rows; i++)
            h[i] = i * Cols;

        for (int k = 0; k < Log; k++) {
            for (int j = 0; j < Cols / 2; j++) {
                for (int i = 0; i < Rows; i++)
                    sortPart1(arrayToBeSorted, arrayThatFollowsTheSort, i
                            * Cols, (i + 1) * Cols, 1, (i % 2 == 0 ? true
                            : false));
                apause(h);
                for (int i = 0; i < Rows; i++)
                    sortPart2(arrayToBeSorted, arrayThatFollowsTheSort, i
                            * Cols, (i + 1) * Cols, 1, (i % 2 == 0 ? true
                            : false));
                apause(h);
            }
            for (int j = 0; j < Rows / 2; j++) {
                for (int i = 0; i < Cols; i++)
                    sortPart1(arrayToBeSorted, arrayThatFollowsTheSort, i, Rows
                            * Cols + i, Cols, true);
                apause(h);
                for (int i = 0; i < Cols; i++)
                    sortPart2(arrayToBeSorted, arrayThatFollowsTheSort, i, Rows
                            * Cols + i, Cols, true);
                apause(h);
            }
        }
        for (int j = 0; j < Cols / 2; j++) {
            for (int i = 0; i < Rows; i++)
                sortPart1(arrayToBeSorted, arrayThatFollowsTheSort, i * Cols,
                        (i + 1) * Cols, 1, true);
            apause(h);
            for (int i = 0; i < Rows; i++)
                sortPart2(arrayToBeSorted, arrayThatFollowsTheSort, i * Cols,
                        (i + 1) * Cols, 1, true);
            apause(h);
        }
        for (int i = 0; i < Rows; i++)
            h[i] = -1;
        apause(h);
    }

    private void sortPart1(double[] arrayToSort, double[] arrayToFollowSort,
            int Lo, int Hi, int Nx, boolean Up) throws Exception {
        for (int j = Lo; j + Nx < Hi; j += 2 * Nx)
            if ((Up && arrayToSort[j] > arrayToSort[j + Nx]) || !Up
                    && arrayToSort[j] < arrayToSort[j + Nx]) {
                double T = arrayToSort[j];
                arrayToSort[j] = arrayToSort[j + Nx];
                arrayToSort[j + Nx] = T;

                /*
                 * the compagnon
                 */
                if (arrayToFollowSort != null) {
                    double Tfollow = arrayToFollowSort[j];
                    arrayToFollowSort[j] = arrayToFollowSort[j + Nx];
                    arrayToFollowSort[j + Nx] = Tfollow;
                }
            }
    }

    private void sortPart2(double[] arrayToSort, double[] arrayToFollowSort,
            int Lo, int Hi, int Nx, boolean Up) throws Exception {
        for (int j = Lo + Nx; j + Nx < Hi; j += 2 * Nx)
            if ((Up && arrayToSort[j] > arrayToSort[j + Nx]) || !Up
                    && arrayToSort[j] < arrayToSort[j + Nx]) {
                double T = arrayToSort[j];
                arrayToSort[j] = arrayToSort[j + Nx];
                arrayToSort[j + Nx] = T;
                /*
                 * the compagnon
                 */
                if (arrayToFollowSort != null) {
                    double Tfollow = arrayToFollowSort[j];
                    arrayToFollowSort[j] = arrayToFollowSort[j + Nx];
                    arrayToFollowSort[j + Nx] = Tfollow;
                }
            }
    }

    private void apause(int H[]) {
        try {
            Thread.sleep(40);
        } catch (InterruptedException e) {
        }
    }

    public static void main(String[] args) {
        ShearSortAlgorithm t = new ShearSortAlgorithm();
        double[] tmp1 = new double[] { 2.5, 4.7, 1.3, 10.123, -0.3, -14.0,
                123.6 };
        double[] tmp2 = new double[] { 2.0, 3.0, 1.0, 4.0, -0.3, -1.0, 5.0 };

        try {
            t.parallelSort(tmp1, tmp2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < tmp2.length; i++) {
            System.out.println(tmp1[i] + " " + tmp2[i]);
        }
    }
}
1245317
private static void quickSort(int[] data, final int start, final int end, Comparator<? super DBIDRef> comp, IntegerDBIDVar vl, IntegerDBIDVar vk, IntegerDBIDVar vr) {
    final int len = end - start;
    if(len < INSERTION_THRESHOLD) {
      // Classic insertion sort.
      for(int i = start + 1; i <= end; i++) {
        for(int j = i; j > start; j--) {
          vl.internalSetIndex(data[j]);
          vr.internalSetIndex(data[j - 1]);
          if(comp.compare(vl, vr) < 0) {
            int tmp = data[j - 1];
            data[j - 1] = data[j];
            data[j] = tmp;
          }
          else {
            break;
          }
        }
      }
      return;
    }

    // Choose pivots by looking at five candidates.
    final int seventh = (len >> 3) + (len >> 6) + 1;
    final int m3 = (start + end) >> 1; // middle
    final int m2 = m3 - seventh;
    final int m1 = m2 - seventh;
    final int m4 = m3 + seventh;
    final int m5 = m4 + seventh;

    // Explicit (and optimal) sorting network for 5 elements
    // See Knuth for details.
    if(compare(vl, data[m1], vk, data[m2], comp) > 0) {
      int tmp = data[m2];
      data[m2] = data[m1];
      data[m1] = tmp;
    }
    if(compare(vl, data[m1], vk, data[m3], comp) > 0) {
      int tmp = data[m3];
      data[m3] = data[m1];
      data[m1] = tmp;
    }
    if(compare(vl, data[m2], vk, data[m3], comp) > 0) {
      int tmp = data[m3];
      data[m3] = data[m2];
      data[m2] = tmp;
    }
    if(compare(vl, data[m4], vk, data[m5], comp) > 0) {
      int tmp = data[m5];
      data[m5] = data[m4];
      data[m4] = tmp;
    }
    if(compare(vl, data[m1], vk, data[m4], comp) > 0) {
      int tmp = data[m4];
      data[m4] = data[m1];
      data[m1] = tmp;
    }
    if(compare(vl, data[m3], vk, data[m4], comp) > 0) {
      int tmp = data[m4];
      data[m4] = data[m3];
      data[m3] = tmp;
    }
    if(compare(vl, data[m2], vk, data[m5], comp) > 0) {
      int tmp = data[m5];
      data[m5] = data[m2];
      data[m2] = tmp;
    }
    if(compare(vl, data[m2], vk, data[m3], comp) > 0) {
      int tmp = data[m3];
      data[m3] = data[m2];
      data[m2] = tmp;
    }
    if(compare(vl, data[m4], vk, data[m5], comp) > 0) {
      int tmp = data[m5];
      data[m5] = data[m4];
      data[m4] = tmp;
    }

    // Choose the 2 and 4th as pivots, as we want to get three parts
    // Copy to variables v1 and v3, replace them with the start and end
    // Note: do not modify v1 or v3 until we put them back!
    vl.internalSetIndex(data[m2]);
    vr.internalSetIndex(data[m4]);
    data[m2] = data[start];
    data[m4] = data[end];

    // A tie is when the two chosen pivots are the same
    final boolean tied = comp.compare(vl, vr) == 0;

    // Insertion points for pivot areas.
    int left = start + 1;
    int right = end - 1;

    // Note: we merged the ties and no ties cases.
    // This likely is marginally slower, but not at a macro level
    // And you never know with hotspot.
    for(int k = left; k <= right; k++) {
      int tmp = data[k];
      vk.internalSetIndex(tmp);
      final int c = comp.compare(vk, vl);
      if(c == 0) {
        continue;
      }
      else if(c < 0) {
        // Traditional quicksort
        data[k] = data[left];
        data[left] = tmp;
        left++;
      }
      else if(tied || comp.compare(vk, vr) > 0) {
        // Now look at the right. First skip correct entries there, too
        while(true) {
          vk.internalSetIndex(data[right]);
          if(comp.compare(vk, vr) > 0 && k < right) {
            right--;
          }
          else {
            break;
          }
        }
        // Now move tmp from k to the right.
        data[k] = data[right];
        data[right] = tmp;
        right--;
        // Test the element we just inserted: left or center?
        vk.internalSetIndex(data[k]);
        if(comp.compare(vk, vl) < 0) {
          tmp = data[k];
          data[k] = data[left];
          data[left] = tmp;
          left++;
        } // else: center. cannot be on right.
      }
    }
    // Put the pivot elements back in.
    // Remember: we must not modify v1 and v3 above.
    data[start] = data[left - 1];
    data[left - 1] = vl.internalGetIndex();
    data[end] = data[right + 1];
    data[right + 1] = vr.internalGetIndex();
    // v1 and v3 are now safe to modify again. Perform recursion:
    quickSort(data, start, left - 2, comp, vl, vk, vr);
    // Handle the middle part - if necessary:
    if(!tied) {
      // TODO: the original publication had a special tie handling here.
      // It shouldn't affect correctness, but probably improves situations
      // with a lot of tied elements.
      quickSort(data, left, right, comp, vl, vk, vr);
    }
    quickSort(data, right + 2, end, comp, vl, vk, vr);
  }
832735
public void sort(@NonNull Comparator<? super T> comparator) {
        final ArrayList<T> copy = new ArrayList<>(mObjects);
        Collections.sort(copy, comparator);
        swap(copy);
    }
1317153
public void permutate(int[] x) {
        for (int i = 0; i < x.length; i++) {
            int j = i + nextInt(x.length - i);
            int s = x[i];
            x[i] = x[j];
            x[j] = s;
        }
    }
1289731
@Override
  public void part(int start, int end) {
    blockNumbers[count][0] = start;
    blockNumbers[count][1] = end - 1;
    count++;
  }
1090666
private static void insertionSort(int array[], int offset, int end) {
		for (int x=offset; x<end; ++x) {
			for (int y=x; y>offset && array[y-1]>array[y]; y--) {
				int temp = array[y];
				array[y] = array[y-1];
				array[y-1] = temp;
			}
		}
	}