Java Median medianAndSort(double[] a)

Here you can find the source of medianAndSort(double[] a)

Description

Returns the median value of input array and sorts array in place.

License

Apache License

Parameter

Parameter Description
a input array; array is sorted in place

Return

median value of a or NaN if a is null

Declaration

public static final double medianAndSort(double[] a) 

Method Source Code

//package com.java2s;
/*/*  ww  w.j a va 2 s .c  o  m*/
 * Copyright 2014 Jon N. Marsh.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Arrays;

public class Main {
    /**
     * Returns the median value of input array and sorts array in place. If the
     * array length is even, the value returned is equal to the average of the
     * middle two values of the sorted array. {@code null} input returns
     * {@code Double.NaN}. Because no array copying is performed, this may yield
     * slight performance improvements over {@link #median(double[]) median} if
     * the input array reordering is unimportant.
     *
     * @param a   input array; array is sorted in place
     * @return median value of {@code a} or {@code NaN} if {@code a} is
     *         {@code null}
     */
    public static final double medianAndSort(double[] a) {
        if (a != null) {
            int n = a.length;
            if (n > 1) {
                int halfN = n / 2;
                Arrays.sort(a);
                if (n % 2 == 0) {
                    return (0.5 * (a[halfN] + a[halfN - 1]));
                } else {
                    return a[halfN];
                }
            } else {
                return a[0];
            }
        } else {
            return Double.NaN;
        }
    }

    /**
     * Returns the median value of input array and sorts array in place. If the
     * array length is even, the value returned is equal to the average of the
     * middle two values of the sorted array. {@code null} input returns
     * {@code Double.NaN}. Because no array copying is performed, this may yield
     * slight performance improvements over {@link #median(float[]) median} if
     * the input array reordering is unimportant.
     *
     * @param a   input array; array is sorted in place
     * @return median value of {@code a} or {@code NaN} if {@code a} is
     *         {@code null}
     */
    public static final float medianAndSort(float[] a) {
        if (a != null) {
            int n = a.length;
            if (n > 1) {
                int halfN = n / 2;
                Arrays.sort(a);
                if (n % 2 == 0) {
                    return (0.5f * (a[halfN] + a[halfN - 1]));
                } else {
                    return a[halfN];
                }
            } else {
                return a[0];
            }
        } else {
            return Float.NaN;
        }
    }
}

Related

  1. median(short[] arr)
  2. median3(double[] v)
  3. median7(double[] v)
  4. median_of_3(int[] x, int x_ptr)
  5. median_sorted(double[] sorted)
  6. medianElement(float[] array, int size)
  7. medianFilter(double[] array, int window)
  8. medianFromHistogram(int[] hist)
  9. medianIndexInSorted(double[] arr)