Java Array Sort sort(int[] idxs, double[] values)

Here you can find the source of sort(int[] idxs, double[] values)

Description

Sort an integer array of indices based on values Updates indices in place, keeps values the same

License

Apache License

Parameter

Parameter Description
idxs indices
values values

Declaration

public static void sort(int[] idxs, double[] values) 

Method Source Code


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

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    /**/*w  ww  . j ava 2 s . com*/
     * Sort an integer array of indices based on values
     * Updates indices in place, keeps values the same
     * @param idxs indices
     * @param values values
     */
    public static void sort(int[] idxs, double[] values) {
        sort(idxs, values, 500);
    }

    public static void sort(int[] idxs, final double[] values, int cutoff) {
        if (idxs.length < cutoff) {
            //hand-rolled insertion sort
            for (int i = 0; i < idxs.length; i++) {
                for (int j = i; j > 0 && values[idxs[j - 1]] > values[idxs[j]]; j--) {
                    int tmp = idxs[j];
                    idxs[j] = idxs[j - 1];
                    idxs[j - 1] = tmp;
                }
            }
        } else {
            Integer[] d = new Integer[idxs.length];
            for (int i = 0; i < idxs.length; ++i)
                d[i] = idxs[i];
            Arrays.sort(d, new Comparator<Integer>() {
                @Override
                public int compare(Integer x, Integer y) {
                    return values[x] < values[y] ? -1 : (values[x] > values[y] ? 1 : 0);
                }
            });
            for (int i = 0; i < idxs.length; ++i)
                idxs[i] = d[i];
        }
    }
}

Related

  1. sort(int[] array)
  2. sort(int[] array)
  3. sort(int[] array)
  4. sort(int[] array)
  5. sort(int[] asd)
  6. Sort(int[] in)
  7. sort(int[] intValues)
  8. sort(int[] keys, int[] values)
  9. sort(int[] values)