Java Array Min Value min(int[] values)

Here you can find the source of min(int[] values)

Description

min

License

Open Source License

Declaration

public static int min(int[] values) 

Method Source Code

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

public class Main {
    public static int min(int[] values) {
        return min(values, 0, values.length);
    }//from www .j a v  a 2  s  .  co  m

    public static int min(int[] values, int begin, int end) {
        int index = minIndex(values, begin, end);
        if (index >= 0 && index < values.length) {
            return values[index];
        } else {
            return Integer.MAX_VALUE;
        }
    }

    public static double min(double[] values) {
        return min(values, 0, values.length);
    }

    public static double min(double[] values, int begin, int end) {
        int index = minIndex(values, begin, end);
        if (index >= 0 && index < values.length) {
            return values[index];
        } else {
            return Double.NaN;
        }
    }

    public static int minIndex(int[] values) {
        return minIndex(values, 0, values.length);
    }

    public static int minIndex(int[] values, int begin, int end) {
        int min = Integer.MAX_VALUE;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            min = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (values[i] < min) {
                    min = values[i];
                    index = i;
                }
            }
        }
        return index;
    }

    public static int minIndex(double[] values) {
        return minIndex(values, 0, values.length);
    }

    public static int minIndex(double[] values, int begin, int end) {
        double min = Double.NaN;
        int index = -1;
        if (checkArguments(values, begin, end)) {
            min = values[begin];
            index = begin;
            for (int i = begin; i < end; i++) {
                if (!Double.isNaN(values[i])) {
                    if (values[i] < min) {
                        min = values[i];
                        index = i;
                    }
                }
            }
        }
        return index;
    }

    private static boolean checkArguments(final int[] values, final int begin, final int end) {
        if (values == null || values.length == 0) {
            return false;
        }
        if (begin < 0 || end >= values.length) {
            return false;
        }
        return true;
    }

    private static boolean checkArguments(final double[] values, final int begin, final int end) {
        if (values == null || values.length == 0) {
            return false;
        }
        if (begin < 0 || end > values.length) {
            return false;
        }
        return true;
    }
}

Related

  1. min(int[] array)
  2. min(int[] array)
  3. min(int[] elements)
  4. min(int[] in, int x1, int x2)
  5. min(int[] list)
  6. min(int[] values)
  7. min(int[] vs)
  8. min(int[] x, int length)
  9. min(long first, long... rest)