Here you can find the source of sort(int[] idxs, double[] values)
Parameter | Description |
---|---|
idxs | indices |
values | values |
public static void sort(int[] idxs, double[] values)
//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]; } } }