Java Median median3(double[] v)

Here you can find the source of median3(double[] v)

Description

median

License

Open Source License

Declaration

private static double median3(double[] v) 

Method Source Code

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

public class Main {
    private static double median3(double[] v) {
        sortInPlace(v, 0, 1);//from   ww  w.j  a v a  2  s. c o  m
        sortInPlace(v, 1, 2);
        sortInPlace(v, 0, 1);
        return v[1];
    }

    private static int median3(int[] v) {
        sortInPlace(v, 0, 1);
        sortInPlace(v, 1, 2);
        sortInPlace(v, 0, 1);
        return v[1];
    }

    private static void sortInPlace(final double[] v, final int i, final int j) {
        if (v[i] > v[j]) {
            final double tmp = v[i];
            v[i] = v[j];
            v[j] = tmp;
        }
    }

    private static void sortInPlace(final int[] v, final int i, final int j) {
        if (v[i] > v[j]) {
            final int tmp = v[i];
            v[i] = v[j];
            v[j] = tmp;
        }
    }
}

Related

  1. median(int[] vals)
  2. median(Integer[] values)
  3. median(long[] array)
  4. median(Number[] array)
  5. median(short[] arr)
  6. median7(double[] v)
  7. median_of_3(int[] x, int x_ptr)
  8. median_sorted(double[] sorted)
  9. medianAndSort(double[] a)