Java Array Sort sortedPointersInto_tryingToImproveSpeed(final double d[])

Here you can find the source of sortedPointersInto_tryingToImproveSpeed(final double d[])

Description

sorted Pointers Inttrying To Improve Speed

License

LGPL

Declaration

public static int[] sortedPointersInto_tryingToImproveSpeed(final double d[]) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.Arrays;

import java.util.Comparator;

public class Main {
    public static int[] sortedPointersInto_tryingToImproveSpeed(final double d[]) {
        /*int pointers[] = new int[d.length];
        for(int i=0; i<d.length; i++) pointers[i] = i;
        //TODO? Arrays.parallelSort(arg0);
        */// w  ww  .j a  v  a  2s  . co m

        for (int i = 0; i < d.length; i++) {
            double x = d[i];
            if (x != x) { //NaN, because it may be causing sorting inconsistency
                d[i] = Double.MAX_VALUE;
            }
        }

        Integer Ints[] = new Integer[d.length];
        for (int i = 0; i < d.length; i++)
            Ints[i] = d.length - 1 - i;
        Comparator<Integer> compare = new Comparator<Integer>() {
            public int compare(Integer x, Integer y) {
                double xd = d[x], yd = d[y];
                if (xd < yd)
                    return -1;
                if (xd > yd)
                    return 1;
                return 0;
            }
        };
        /*while(true){
           try{
        Arrays.sort(Ints, compare);
        break;
           }catch(Exception e){
        System.out.println("This is probably 'Comparison method violates its general contract' which strictfp avoids always singlethreaded but it appears some thread is using it, but which one could it be since its a local var? For now, since it happens only 1 20000 times its faster to just catch this and do it again those times. TODO find that thread and synchronize here and there! "+e.getMessage());
        e.printStackTrace(System.out);
           }
        }*/
        Arrays.sort(Ints, compare);
        int ints[] = new int[d.length];
        for (int i = 0; i < d.length; i++)
            ints[i] = Ints[i];
        return ints;
    }
}

Related

  1. sortedIndex(double[] values)
  2. sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
  3. sortedMerge(int[] a, int[] b)
  4. sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
  5. sortedPointersInto(double d[])
  6. sortedPointersInto_usingStrictfp(final double d[])
  7. sortEigenValues(int n, double[] d, double[][] v)
  8. sortIndex(double[] A)
  9. sortIndex(double[] doubleArray)