Java Median median7(double[] v)

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

Description

median

License

Open Source License

Declaration

private static double median7(double[] v) 

Method Source Code

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

public class Main {
    private static double median7(double[] v) {
        sortInPlace(v, 0, 5);/*from   www. ja v  a  2 s  .c o m*/
        sortInPlace(v, 0, 3);
        sortInPlace(v, 1, 6);
        sortInPlace(v, 2, 4);
        sortInPlace(v, 0, 1);
        sortInPlace(v, 3, 5);
        sortInPlace(v, 2, 6);
        sortInPlace(v, 2, 3);
        sortInPlace(v, 3, 6);
        sortInPlace(v, 4, 5);
        sortInPlace(v, 1, 4);
        sortInPlace(v, 1, 3);
        sortInPlace(v, 3, 4);
        return v[3];
    }

    private static int median7(int[] v) {
        sortInPlace(v, 0, 5);
        sortInPlace(v, 0, 3);
        sortInPlace(v, 1, 6);
        sortInPlace(v, 2, 4);
        sortInPlace(v, 0, 1);
        sortInPlace(v, 3, 5);
        sortInPlace(v, 2, 6);
        sortInPlace(v, 2, 3);
        sortInPlace(v, 3, 6);
        sortInPlace(v, 4, 5);
        sortInPlace(v, 1, 4);
        sortInPlace(v, 1, 3);
        sortInPlace(v, 3, 4);
        return v[3];
    }

    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(Integer[] values)
  2. median(long[] array)
  3. median(Number[] array)
  4. median(short[] arr)
  5. median3(double[] v)
  6. median_of_3(int[] x, int x_ptr)
  7. median_sorted(double[] sorted)
  8. medianAndSort(double[] a)
  9. medianElement(float[] array, int size)