Java Array Min Value argmin(int[] a)

Here you can find the source of argmin(int[] a)

Description

argmin

License

Apache License

Declaration

public static int argmin(int[] a) 

Method Source Code

//package com.java2s;
/**/*from w  ww .ja  va 2 s.  c o  m*/
 * 
 *    Copyright 2017 Florian Erhard
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

public class Main {
    public static int argmin(int[] a) {
        int re = Integer.MAX_VALUE;
        int arg = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] < re) {
                re = a[i];
                arg = i;
            }
        }
        return arg;
    }

    public static int argmin(float[] a) {
        float re = Float.POSITIVE_INFINITY;
        int arg = -1;
        for (int i = 0; i < a.length; i++) {
            if (a[i] < re) {
                re = a[i];
                arg = i;
            }
        }
        return arg;
    }

    /**
     * Computes the argmin of the given array.
     * @param array the array
     * @param from start index (inclusive)
     * @param to end index (exclusive)
     * @return the argmin
     */
    public static int argmin(double[] array, int from, int to) {
        if (from >= to)
            return -1;
        double max = array[from];
        int re = from;
        for (int i = from + 1; i < to; i++) {
            if (array[i] < max) {
                max = array[i];
                re = i;
            }
        }
        return re;
    }

    /**
     * Computes the argmin of the given array.
     * @param array the array
     * @return the argmin
     */
    public static int argmin(double[] array) {
        double min = array[0];
        int re = 0;
        for (int i = 1; i < array.length; i++) {
            if (array[i] < min) {
                min = array[i];
                re = i;
            }
        }
        return re;
    }
}

Related

  1. argmin(double... vs)
  2. argmin(final double[] vec)
  3. argMin(int[] a)
  4. arrayMin(double minVal, double[] vals)
  5. arrayMin(double[] arr)
  6. arrayMin(double[] x)