Java Median median(double[] array)

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

Description

Calculates the median of an array by sorting using QuickSort and taking the middle element if the length of the array is odd and the average of the two middle elements otherwise.

License

Open Source License

Parameter

Parameter Description
array a parameter

Declaration

public static double median(double[] array) 

Method Source Code

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

public class Main {
    /**//from w  w  w. ja va2 s  .c  o  m
     * Calculates the median of an array by sorting using QuickSort and taking
     * the middle element if the length of the array is odd and the average of
     * the two middle elements otherwise. Does not check for valid inputs.
     * 
     * @param array
     * @return
     */
    public static double median(double[] array) {
        int len = array.length;
        int mid = len / 2;
        quickSort(array, 0, len - 1);
        // Median is the midpoint of the sorted list
        if (len % 2 == 0) {
            return .5 * (array[mid - 1] + array[mid]);
        } else {
            return array[mid];
        }

    }

    /**
     * Quicksort filter. Use low = 0 and high = length - 1 to sort the whole
     * array. From <a href=
     * "http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html"
     * >http://www.vogella.com/articles/JavaAlgorithmsQuicksort/article.html<a>
     * 
     * @param array The array to sort.
     * @param low Starting datasetIndex of the part of the array to sort.
     * @param low Ending datasetIndex of the part of the array to sort.
     */
    public static void quickSort(double[] array, int low, int high) {
        int i = low, j = high;
        // Get the pivot element from the middle of the list
        double pivot = array[low + (high - low) / 2];

        // Divide into two lists
        double exchange;
        while (i <= j) {
            // If the current value from the left list is smaller then the pivot
            // element then get the next element from the left list
            while (array[i] < pivot) {
                i++;
            }
            // If the current value from the right list is larger then the pivot
            // element then get the next element from the right list
            while (array[j] > pivot) {
                j--;
            }

            // If we have found a values in the left list which is larger then
            // the pivot element and if we have found a value in the right list
            // which is smaller then the pivot element then we exchange the
            // values.
            // As we are done we can increase i and j
            if (i <= j) {
                exchange = array[i];
                array[i] = array[j];
                array[j] = exchange;
                i++;
                j--;
            }
        }
        // Recursion
        if (low < j)
            quickSort(array, low, j);
        if (i < high)
            quickSort(array, i, high);
    }
}

Related

  1. Median(ArrayList values)
  2. median(double... a)
  3. median(double[] a)
  4. median(double[] a)
  5. median(double[] arr)
  6. median(double[] array)
  7. median(double[] data)
  8. median(double[] data, int length)
  9. median(double[] input)