/** * Sorts the {@code group} and {@code values} array in tandem and calculates the statistic per group. * * <p>Note: the {@code group} and {@code values} array are sorted in place!</p> * * @param statistic the statistic to apply * @param groupId an array of at least length {@code numValues} containing group ids * @param values an array of at least length {@code numValues} containing the values to be aggregated * @param numValues number of values to aggregate. * @param numGroups number of groups present. The maximum value in the groupId values should be {@code numGroups-1} * @return an array of length {@code numGroups} containing the aggregated values. */ public static double[] sortAndAggregate(StatFunction statistic, int[] groupId, double[] values, int numValues, int numGroups) { if(numValues > 1) { HeapsortTandem.heapsortDescending(groupId, values, numValues); } return aggregateSorted(statistic, groupId, values, numValues, numGroups); }
@Test public void doubleSortedByIndexDsc() { int id[] = {5, 3, 2, 6, 1, 4, 7, 10, 9, 8 }; double values[] = { 50, 150, 90, 3, 9, 10, 30, 4, 5, 6 }; System.out.println(Arrays.toString(id)); System.out.println(Arrays.toString(values)); HeapsortTandem.heapsortDescending(id, values, 10); System.out.println("Sorted (D): " + Arrays.toString(id)); System.out.println("Tandem (D): " + Arrays.toString(values)); }
@Test public void stringSortedByIndexDsc() { String stringVals[] = {"c", "a", "b", "s"}; int stringIndex[] = {2, 3, 1, 4}; System.out.println(Arrays.toString(stringIndex)); System.out.println(Arrays.toString(stringVals)); HeapsortTandem.heapsortDescending(stringIndex, stringVals, 4); System.out.println("Sorted (D): " + Arrays.toString(stringIndex)); System.out.println("Tandem (D): " + Arrays.toString(stringVals)); }
@Test public void sortedDsc() { int id[] = {-1, -1, -1, -1, -1, -1, -1, 0, 0, 0 }; double values[] = { 50, 150, 90, 3, 9, 10, 30, 4, 5, 6 }; System.out.println(Arrays.toString(id)); System.out.println(Arrays.toString(values)); HeapsortTandem.heapsortDescending(id, values, 10); System.out.println("Sorted (D): " + Arrays.toString(id)); System.out.println("Tandem (D): " + Arrays.toString(values)); assertThat( values[0] + values[1] + values[2], equalTo(15d)); }
HeapsortTandem.heapsortDescending(groupArray, filteredValues, filteredValues.length);