Java Array Sort sortIndex(double[] doubleArray)

Here you can find the source of sortIndex(double[] doubleArray)

Description

sort Index

License

Open Source License

Declaration

public static int[] sortIndex(double[] doubleArray) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static int[] sortIndex(double[] doubleArray) {
        // Get the index of a sorted double array
        return sortIndex(_arrayToArrayList(doubleArray));
    }//from   w w  w  .j a  va 2s  .c  o m

    public static int[] sortIndex(ArrayList<Double> doubleArrayList) {
        // Get the index of a sorted Double ArrayList
        ArrayList<Double> sortedArrayList = new ArrayList<Double>(doubleArrayList);
        Collections.sort(sortedArrayList);
        ArrayList<Double> copyArrayList = new ArrayList<Double>(doubleArrayList);
        int length = sortedArrayList.size();
        int[] sortedIndices = new int[length];
        Arrays.fill(sortedIndices, -1);

        for (int i = 0; i < length; i++) {
            // Traverse the value in descending value
            Double ithValue = sortedArrayList.get(length - 1 - i);
            while (_contains(sortedIndices, copyArrayList.indexOf(ithValue))) {
                copyArrayList.set(copyArrayList.indexOf(ithValue), -1.);
            }
            sortedIndices[i] = copyArrayList.indexOf(ithValue);
        }
        return sortedIndices;
    }

    private static ArrayList<Double> _arrayToArrayList(double[] doubleArray) {
        ArrayList<Double> doubleArrayList = new ArrayList<Double>();
        for (int i = 0; i < doubleArray.length; i++) {
            doubleArrayList.add(doubleArray[i]);
        }
        return doubleArrayList;
    }

    private static boolean _contains(int[] intArray, int val) {
        for (int intVal : intArray) {
            if (intVal == val) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. sortedPointersInto(double d[])
  2. sortedPointersInto_tryingToImproveSpeed(final double d[])
  3. sortedPointersInto_usingStrictfp(final double d[])
  4. sortEigenValues(int n, double[] d, double[][] v)
  5. sortIndex(double[] A)
  6. sortIndexes(final T[] array)
  7. sortIndexesDescending(final double[] in)
  8. sortindices(double[] x)
  9. sortIndices(float[] main)