java.lang.Object
imagingbook.common.util.PrimitiveSortMap
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
ConstructorsConstructorDescriptionPrimitiveSortMap
(double[] a) Constructor fordouble[]
.PrimitiveSortMap
(float[] a) Constructor forfloat[]
.PrimitiveSortMap
(int[] a) Constructor forint[]
. -
Method Summary
Modifier and TypeMethodDescriptionint
getIndex
(int n) Returns the index of the n-th smallest element of the original number sequence (passed to the constructor), starting withn = 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 specifiedint[]
, starting withn = 0
, which returns the smallest element ofnumbers
.int[]
Returns the permutation (position indexes) for the underlying number sequence as aint
array.
-
Constructor Details
-
PrimitiveSortMap
Constructor fordouble[]
. The input array is not modified.- Parameters:
a
- the input array
-
PrimitiveSortMap
Constructor forfloat[]
. The input array is not modified.- Parameters:
a
- the input array
-
PrimitiveSortMap
Constructor forint[]
. The input array is not modified.- Parameters:
a
- the input array
-
-
Method Details
-
getPermutation
Returns the permutation (position indexes) for the underlying number sequence as aint
array. That is, the first value in the returned array is the index of the smallest element innumbers
, the second element points to the second-smallest element, etc. For example, if the original number sequence (passed to the constructor) isdouble[] a = {50.0, 20.0, 100.0, 120.0, 40.0, -10.0};
then the index array produced byint[] perm = new PrimitiveSortMap(a).getPermutation();
contains(5, 1, 4, 0, 2, 3)
. This means thata[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
Returns the index of the n-th smallest element of the original number sequence (passed to the constructor), starting withn = 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
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 valuesn
- the index- Returns:
- the n-th smallest element
-
getNthSmallestIndex
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 valuesn
- the index- Returns:
- the index of the n-th smallest element
-
getLargestValue
Returns the largest element of the specified array.- Parameters:
numbers
- sequence of unsorted values- Returns:
- the largest element
-
getLargestIndex
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
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 valuesn
- the index- Returns:
- the n-th smallest element
-
getNthSmallestIndex
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 valuesn
- the index- Returns:
- the index of the n-th smallest element
-
getLargestValue
Returns the largest element of the specified array.- Parameters:
numbers
- sequence of unsorted values- Returns:
- the largest element
-
getLargestIndex
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
Returns the nth smallest element of the specifiedint[]
, starting withn = 0
, which returns the smallest element ofnumbers
.- Parameters:
numbers
- sequence of unsorted valuesn
- the index- Returns:
- the n-th smallest element of
numbers
-
getNthSmallestIndex
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 valuesn
- the index- Returns:
- the index of the n-th smallest element
-
getLargestValue
Returns the largest element of the specified array.- Parameters:
numbers
- sequence of unsorted values- Returns:
- the largest element
-
getLargestIndex
Returns the index of the largest element of the specified array.- Parameters:
numbers
- sequence of unsorted values- Returns:
- the index of the largest element
-