Example usage for java.util Arrays sort

List of usage examples for java.util Arrays sort

Introduction

In this page you can find the example usage for java.util Arrays sort.

Prototype

public static void sort(Object[] a) 

Source Link

Document

Sorts the specified array of objects into ascending order, according to the Comparable natural ordering of its elements.

Usage

From source file:es.ucm.fdi.ac.outlier.Hampel.java

/**
 * median of an array/*from w  w  w. ja v  a 2  s.c o m*/
 */
public static double median(double[] array) {
    Arrays.sort(array); // sort the elements
    boolean even = (array.length % 2) == 0;
    double median = 0;

    if (even) {
        int rightNumber = array.length / 2;
        int leftNumber = rightNumber - 1;
        // return average of two center-elements
        median = (array[rightNumber] + array[leftNumber]) / 2;
    } else {
        // choose the center one            
        median = array[array.length / 2];
    }

    return median;
}