Java Median median(double[] input)

Here you can find the source of median(double[] input)

Description

Return the median of the array

License

Apache License

Declaration

static public double median(double[] input) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from w w w .  jav a  2 s.com
     * Return the median of the array
     */
    static public double median(double[] input) {
        double res = 0;
        int[] index = new int[input.length];
        int len = input.length;
        for (int i = 0; i < len; i++)
            index[i] = i;
        bubbleSort(input, index);

        if ((len % 2) == 1) {
            res = input[len / 2];
        } else {
            res = (input[len / 2] + input[len / 2 - 1]) / 2;
        }
        return res;
    }

    /**
     * Bubble Sort for Integer array
     * 
     * @param array
     *            Value of the original array
     * @param index
     *            Index of the original array
     * @return Array is sorted in ascending order (lowest to highest). Together
     *         with its original index stored in index[]
     */
    static public int[] bubbleSort(int array[], int index[]) {
        boolean swappedOnPrevRun = true;
        while (swappedOnPrevRun) {
            swappedOnPrevRun = false;
            // this variable keeps track of whether to continue sorting or exit
            for (int i = 0; i < array.length - 1; i++)
            // loop through every element in the array,
            // except for the last one
            {
                if (array[i] > array[i + 1])
                // if current element is greater than the next
                {
                    // swap the two elements
                    swappedOnPrevRun = true; // we don't want the loop to end
                    // just yet, we're not done
                    int temp = array[i]; // store element i in a temporary
                                         // variable
                    array[i] = array[i + 1]; // set element i+1 to where i used
                    // to be
                    array[i + 1] = temp; // release the old i from temp into i+1
                                         // slot

                    temp = index[i];
                    index[i] = index[i + 1];
                    index[i + 1] = temp;
                }
            }
        }
        return array;
    }

    /**
     * Bubble Sort for Double array
     * 
     * @param array
     *            Value of the original array
     * @param index
     *            Index of the original array
     * @return Array is sorted in ascending order (lowest to highest). Together
     *         with its original index stored in index[]
     */
    static public double[] bubbleSort(double array[], int index[]) {
        boolean swappedOnPrevRun = true;
        while (swappedOnPrevRun) {
            swappedOnPrevRun = false;
            // this variable keeps track of whether to continue sorting or exit
            for (int i = 0; i < array.length - 1; i++)
            // loop through every element in the array,
            // except for the last one
            {
                if (array[i] > array[i + 1])
                // if current element is greater than the next
                {
                    // swap the two elements
                    swappedOnPrevRun = true; // we don't want the loop to end
                    // just yet, we're not done
                    double temp = array[i]; // store element i in a temporary
                    // variable
                    array[i] = array[i + 1]; // set element i+1 to where i used
                    // to be
                    array[i + 1] = temp; // release the old i from temp into i+1
                                         // slot

                    int temp1 = index[i];
                    index[i] = index[i + 1];
                    index[i + 1] = temp1;
                }
            }
        }
        return array;
    }
}

Related

  1. median(double[] arr)
  2. median(double[] array)
  3. median(double[] array)
  4. median(double[] data)
  5. median(double[] data, int length)
  6. median(double[] l)
  7. median(double[] unsorted)
  8. median(double[] v)
  9. median(double[] vals)