Java Median median(int x[], int pos1, int pos2, int pos3)

Here you can find the source of median(int x[], int pos1, int pos2, int pos3)

Description

median

License

Open Source License

Declaration

private static int median(int x[], int pos1, int pos2, int pos3) 

Method Source Code

//package com.java2s;
/**//w ww  . jav a 2s  . c  om
 * ****************************************************************************
 * Copyright (c) 2008 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * SAP AG - initial API and implementation
 * *****************************************************************************
 */

public class Main {
    private static int median(int x[], int pos1, int pos2, int pos3) {
        int v1 = x[pos1];
        int v2 = x[pos2];
        int v3 = x[pos3];

        if (v1 < v2) {
            if (v2 <= v3) {
                return pos2;
            } else {
                return v1 < v3 ? pos3 : pos1;
            }
        }

        /* else -> v1 > v2 */
        if (v1 <= v3) {
            return pos1;
        } else {
            return v2 < v3 ? pos3 : pos2;
        }
    }

    private static int median(long x[], int pos1, int pos2, int pos3) {
        long v1 = x[pos1];
        long v2 = x[pos2];
        long v3 = x[pos3];

        if (v1 < v2) {
            if (v2 <= v3) {
                return pos2;
            } else {
                return v1 < v3 ? pos3 : pos1;
            }
        }

        /* else -> v1 > v2 */
        if (v1 <= v3) {
            return pos1;
        } else {
            return v2 < v3 ? pos3 : pos2;
        }
    }
}

Related

  1. median(final int[] values)
  2. median(float a, float b, float c)
  3. median(float[] vals)
  4. median(float[] values)
  5. median(float[] vector)
  6. median(int[] m)
  7. median(int[] vals)
  8. median(Integer[] values)
  9. median(long[] array)