Java Median medianElement(float[] array, int size)

Here you can find the source of medianElement(float[] array, int size)

Description

Calculate the median value of a float array.

License

Open Source License

Parameter

Parameter Description
array a parameter
size a parameter

Declaration

public static float medianElement(float[] array, int size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w ww  . j a  v a2s .c  o  m*/
     * Calculate the median value of a float array.
     * 
     * This is currently done in a stupidly inefficient way
     * using bubble sort.
     * 
     * TODO: Need to implement a proper algorithm
     * to find the median
     * 
     * @param array
     * @param size
     * @return
     */
    public static float medianElement(float[] array, int size) {
        float[] tmpArray = new float[size];
        System.arraycopy(array, 0, tmpArray, 0, size);
        boolean changed = true;
        while (changed) {
            changed = false;
            for (int i = 0; i < size - 1; i++) {
                if (tmpArray[i] > tmpArray[i + 1]) {
                    changed = true;
                    float tmp = tmpArray[i];
                    tmpArray[i] = tmpArray[i + 1];
                    tmpArray[i + 1] = tmp;
                }
            }
        }
        return tmpArray[size / 2];
    }
}

Related

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