Class PrimitiveSortMap

java.lang.Object
imagingbook.common.util.PrimitiveSortMap

public class PrimitiveSortMap extends Object

Determines the 'permutation' of a sequence of numbers and keeps it as an array (perm) of position indexes. These indexes indicate how the original input array may be re-ordered to become sorted (see getPermutation()). Implemented only for arrays of type int, float and double.

Usage example - get the second-smallest element of a double array:

        double[] a = ...        // some array
        int k = PrimitiveSortMap.getNthSmallestIndex(a, 1);     // index of 2nd-smallest element
        double x = a[k];
 

Alternatively, the 2nd-smallest value can be obtained directly by

 double x = PrimitiveSortMap.getNthSmallestValue(a, 1);
 
Version:
2022/09/15
  • Constructor Summary

    Constructors
    Constructor
    Description
    PrimitiveSortMap(double[] a)
    Constructor for double[].
    PrimitiveSortMap(float[] a)
    Constructor for float[].
    Constructor for int[].
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getIndex(int n)
    Returns the index of the n-th smallest element of the original number sequence (passed to the constructor), starting with n = 0, which returns the index of the smallest element.
    static int
    getLargestIndex(double[] numbers)
    Returns the index of the largest element of the specified array.
    static int
    getLargestIndex(float[] numbers)
    Returns the index of the largest element of the specified array.
    static int
    getLargestIndex(int[] numbers)
    Returns the index of the largest element of the specified array.
    static double
    getLargestValue(double[] numbers)
    Returns the largest element of the specified array.
    static float
    getLargestValue(float[] numbers)
    Returns the largest element of the specified array.
    static float
    getLargestValue(int[] numbers)
    Returns the largest element of the specified array.
    static int
    getNthSmallestIndex(double[] numbers, int n)
    Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
    static int
    getNthSmallestIndex(float[] numbers, int n)
    Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
    static int
    getNthSmallestIndex(int[] numbers, int n)
    Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
    static double
    getNthSmallestValue(double[] numbers, int n)
    Returns the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
    static float
    getNthSmallestValue(float[] numbers, int n)
    Returns the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
    static int
    getNthSmallestValue(int[] numbers, int n)
    Returns the nth smallest element of the specified int[], starting with n = 0, which returns the smallest element of numbers.
    int[]
    Returns the permutation (position indexes) for the underlying number sequence as a int array.

    Methods inherited from class java.lang.Object

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

    • PrimitiveSortMap

      public PrimitiveSortMap(double[] a)
      Constructor for double[]. The input array is not modified.
      Parameters:
      a - the input array
    • PrimitiveSortMap

      public PrimitiveSortMap(float[] a)
      Constructor for float[]. The input array is not modified.
      Parameters:
      a - the input array
    • PrimitiveSortMap

      public PrimitiveSortMap(int[] a)
      Constructor for int[]. The input array is not modified.
      Parameters:
      a - the input array
  • Method Details

    • getPermutation

      public int[] getPermutation()
      Returns the permutation (position indexes) for the underlying number sequence as a int array. That is, the first value in the returned array is the index of the smallest element in numbers, the second element points to the second-smallest element, etc. For example, if the original number sequence (passed to the constructor) is
           double[] a = {50.0, 20.0, 100.0, 120.0, 40.0, -10.0};
       
      then the index array produced by
           int[] perm = new PrimitiveSortMap(a).getPermutation();
       
      contains (5, 1, 4, 0, 2, 3). This means that
           a[perm[i]] ≤ a[perm[i+1]]
       
      and thus the sequence
           {a[perm[0]], A[perm[1]],..., A[perm[N-1]]}
       
      is sorted.
      Returns:
      the sorting map (permutation)
    • getIndex

      public int getIndex(int n)
      Returns the index of the n-th smallest element of the original number sequence (passed to the constructor), starting with n = 0, which returns the index of the smallest element.
      Parameters:
      n - the index
      Returns:
      the index of the n-th smallest element in the original number sequence
    • getNthSmallestValue

      public static double getNthSmallestValue(double[] numbers, int n)
      Returns the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the n-th smallest element
    • getNthSmallestIndex

      public static int getNthSmallestIndex(double[] numbers, int n)
      Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the index of the n-th smallest element
    • getLargestValue

      public static double getLargestValue(double[] numbers)
      Returns the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the largest element
    • getLargestIndex

      public static int getLargestIndex(double[] numbers)
      Returns the index of the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the index of the largest element
    • getNthSmallestValue

      public static float getNthSmallestValue(float[] numbers, int n)
      Returns the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the n-th smallest element
    • getNthSmallestIndex

      public static int getNthSmallestIndex(float[] numbers, int n)
      Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the index of the n-th smallest element
    • getLargestValue

      public static float getLargestValue(float[] numbers)
      Returns the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the largest element
    • getLargestIndex

      public static int getLargestIndex(float[] numbers)
      Returns the index of the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the index of the largest element
    • getNthSmallestValue

      public static int getNthSmallestValue(int[] numbers, int n)
      Returns the nth smallest element of the specified int[], starting with n = 0, which returns the smallest element of numbers.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the n-th smallest element of numbers
    • getNthSmallestIndex

      public static int getNthSmallestIndex(int[] numbers, int n)
      Returns the index of the nth smallest element of the specified array, starting with n = 0, which returns the smallest element of the array.
      Parameters:
      numbers - sequence of unsorted values
      n - the index
      Returns:
      the index of the n-th smallest element
    • getLargestValue

      public static float getLargestValue(int[] numbers)
      Returns the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the largest element
    • getLargestIndex

      public static int getLargestIndex(int[] numbers)
      Returns the index of the largest element of the specified array.
      Parameters:
      numbers - sequence of unsorted values
      Returns:
      the index of the largest element