Class IndirectSort

java.lang.Object
com.carrotsearch.hppc.sorting.IndirectSort

public final class IndirectSort extends Object
Sorting routines that return an array of sorted indices implied by a given comparator rather than move elements of whatever the comparator is using for comparisons.

A practical use case for this class is when the index of an array is meaningful and one wants to acquire the order of values in that array. None of the methods in Java Collections would provide such functionality directly and creating a collection of boxed Integer objects for indices seems to be too costly.

  • Method Summary

    Modifier and Type
    Method
    Description
    static int[]
    mergesort(int[] orderArray, IntBinaryOperator comparator)
    Returns a sorted copy of the order array provided, using the given comparator.
    static int[]
    mergesort(int start, int length, IntBinaryOperator comparator)
    Returns the order of elements between indices start and length, as indicated by the given comparator.
    static <T> int[]
    mergesort(T[] input, int start, int length, Comparator<? super T> comparator)
    Returns the order of elements between indices start and length, as indicated by the given comparator.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • mergesort

      public static int[] mergesort(int start, int length, IntBinaryOperator comparator)
      Returns the order of elements between indices start and length, as indicated by the given comparator.

      This routine uses merge sort. It is guaranteed to be stable. It creates a new indices array, and clones it while sorting.

    • mergesort

      public static int[] mergesort(int[] orderArray, IntBinaryOperator comparator)
      Returns a sorted copy of the order array provided, using the given comparator.

      This routine uses merge sort. It is guaranteed to be stable. The provided indicesArray is cloned while sorting and the clone is returned.

    • mergesort

      public static <T> int[] mergesort(T[] input, int start, int length, Comparator<? super T> comparator)
      Returns the order of elements between indices start and length, as indicated by the given comparator.

      This routine uses merge sort. It is guaranteed to be stable. It creates a new indices array, and clones it while sorting.