Java Array Sort sortedPointersInto(double d[])

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

Description

sorted Pointers Into

License

LGPL

Declaration

public static int[] sortedPointersInto(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(double d[]) {
        return sortedPointersInto_tryingToImproveSpeed(d);
    }/*w w w.  j  ava  2s.c om*/

    public static strictfp int[] sortedPointersInto(final long d[]) {
        Integer Ints[] = new Integer[d.length];
        for (int i = 0; i < d.length; i++)
            Ints[i] = i;
        Comparator<Integer> compare = new Comparator<Integer>() {
            public int compare(Integer x, Integer y) {
                long xd = d[x], yd = d[y];
                if (xd < yd)
                    return -1;
                if (xd > yd)
                    return 1;
                return 0;
            }
        };
        Arrays.sort(Ints, compare);
        int ints[] = new int[d.length];
        for (int i = 0; i < d.length; i++)
            ints[i] = Ints[i];
        return ints;
    }

    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);
        */

        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. sortedCopyOf(final int[] array)
  2. sortedIndex(double[] values)
  3. sortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
  4. sortedMerge(int[] a, int[] b)
  5. sortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
  6. sortedPointersInto_tryingToImproveSpeed(final double d[])
  7. sortedPointersInto_usingStrictfp(final double d[])
  8. sortEigenValues(int n, double[] d, double[][] v)
  9. sortIndex(double[] A)