Package com.landawn.abacus.util
Class Median
java.lang.Object
com.landawn.abacus.util.Median
- Author:
- Haiyang Li
-
Method Summary
Modifier and TypeMethodDescriptionstatic Pair<Byte,
u.OptionalByte> of
(byte... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Byte,
u.OptionalByte> of
(byte[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Character,
u.OptionalChar> of
(char... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Character,
u.OptionalChar> of
(char[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Double,
u.OptionalDouble> of
(double... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Double,
u.OptionalDouble> of
(double[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Float,
u.OptionalFloat> of
(float... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Float,
u.OptionalFloat> of
(float[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Integer,
u.OptionalInt> of
(int... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Integer,
u.OptionalInt> of
(int[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Long,
u.OptionalLong> of
(long... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Long,
u.OptionalLong> of
(long[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Short,
u.OptionalShort> of
(short... a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static Pair<Short,
u.OptionalShort> of
(short[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static <T extends Comparable<? super T>>
Pair<T,u.Nullable<T>> of
(Collection<? extends T> c) static <T> Pair<T,
u.Nullable<T>> of
(Collection<? extends T> c, Comparator<? super T> cmp) static <T extends Comparable<? super T>>
Pair<T,u.Nullable<T>> of
(T[] a) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static <T extends Comparable<? super T>>
Pair<T,u.Nullable<T>> of
(T[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static <T> Pair<T,
u.Nullable<T>> of
(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.static <T> Pair<T,
u.Nullable<T>> of
(T[] a, Comparator<? super T> cmp) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.
-
Method Details
-
of
@SafeVarargs public static Pair<Character,u.OptionalChar> of(char... a) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
@SafeVarargs public static Pair<Short,u.OptionalShort> of(short... a) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.
The input array don't need to be sorted. in other words, there is no benefit even if the array is sorted.Median.of(1); // -> [1, empty] Median.of(1, 3); // -> [1, 3] Median.of(1, 3, 5); // -> [3, empty] Median.of(1, 1); // -> [1, 1] Median.of(1, 1, 3); // -> [1, empty] Median.of(1, 1, 3, 5); // -> [1, 3] Median.of(1, 1, 1, 3, 5); // -> [1, empty] Median.of(1, 1, 1, 3, 3, 5); // -> [1, 3] Median.of(1, 1, 1, 3, 3, 3, 5); // -> [3, empty]
- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- Throws:
IllegalArgumentException
- if specified array is empty or length is zero.
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
@SafeVarargs public static Pair<Float,u.OptionalFloat> of(float... a) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
@SafeVarargs public static Pair<Double,u.OptionalDouble> of(double... a) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
public static <T extends Comparable<? super T>> Pair<T,u.Nullable<T>> of(T[] a) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Type Parameters:
T
-- Parameters:
a
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
public static <T extends Comparable<? super T>> Pair<T,u.Nullable<T>> of(T[] a, int fromIndex, int toIndex) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Type Parameters:
T
-- Parameters:
a
-fromIndex
-toIndex
-- Returns:
- See Also:
-
of
public static <T> Pair<T,u.Nullable<T>> of(T[] a, Comparator<? super T> cmp) throws IllegalArgumentException Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Type Parameters:
T
-- Parameters:
a
-cmp
-- Returns:
- Throws:
IllegalArgumentException
- if the specifiedArray
is null or empty.- See Also:
-
of
public static <T> Pair<T,u.Nullable<T>> of(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) Returns aPair
withleft
is the value at the index of(a.length - 1) / 2
when the array is sorted, and theright
is the value at the index ofa.length / 2
if the length of array is even.- Type Parameters:
T
-- Parameters:
a
-fromIndex
-toIndex
-cmp
-- Returns:
- See Also:
-
of
- Type Parameters:
T
-- Parameters:
c
-- Returns:
-
of
- Type Parameters:
T
-- Parameters:
c
-cmp
-- Returns:
-