0

I'm trying to find the minimum value of numbers in an array but it doesn't always work out properly. This is the code I wrote:

for (int i=0; i < arr.length; i++) {
    min = arr[i];
    for (int j=0; j < arr.length; j++) {
        if (arr[j] < arr[0]) {
            min = arr[j];
        }
    }
}   

Can someone explain to me what I did wrong and how I can improve the code?

3
  • what is the use of your outer for loop Commented Dec 5, 2014 at 13:36
  • 1
    arr[j] < arr[0] should be arr[j] < min. Commented Dec 5, 2014 at 13:37
  • 1
    possible duplicate of Java: Max/min value in an array? Commented Dec 5, 2014 at 13:43

10 Answers 10

3

There's no need for the outer loop, it only runs once and you don't use i anyway. why do you have it?

For the inner loop, you need to compare against the minimum value. Right now you are comparing it against the first element in the array, which is not necessarily the minimum value.

min = arr[0];
for (j=0; j < arr.length; j++) {
    if (arr[j] < min) {  //<---fix is here
        min = arr[j];
    }
}

Also you could start the loop at 1, since you don't need to compare arr[0] against itself (it was just assigned to min)

Sign up to request clarification or add additional context in comments.

Comments

2
int min = arr[0];
for(int num : arr) {
    if (num < min){
        min = num;
    }
}

min now contains minimum value.

1 Comment

to be safe I would add a null check, and an array length check.
2

If arr is an array of non-primitive numerics, I'd recommend

java.util.Collections.min(java.util.Arrays.asList(arr));

as that will be simpler to maintain. There's some justification in reverting to hand-coding if you need to pull the minimum and maximum out at the same time but I'd advise against hand-coding loops if there's a library function available.

In any case, you ought to check arr != null and the existence of a zeroth element.

Comments

1

A way to do it would be using the java.util.Arrays class:

Example:

public class ArraySort {
public static void main(String[] args) {
    int[] array = {12, 4, 6, 1, 56, 21, 77};
    Arrays.sort(array);
    System.out.println(array[0]);
}
}

From the Java doc, Arrays.sort(int[]) sort the specified array into ascending numerical order.

So the output here prints 1.

1 Comment

You are performing a sorting operation (O(nlogn)) but you could do it in linear time.
1

One option is sort your array and get the first element:

import java.util.Arrays;

...

int ints[] = {30,5,7,4,10};
Arrays.sort(ints);

int min = ints[0];
int max = ints[ints.length - 1];

Comments

1

You are checking the first element in each iteration, you basically need to check minimum value

if (arr[j] < min) {
  min = arr[j];
}

Comments

0

Here is a general algorithm for doing this. You can write the code for it.

Store the first item in the array as the current minimum.

Loop through the array, starting at the second item (index 1).

For each iteration of the array, check if the current item is less than the minimum. If it is, store it as the new minimum.

Once the loop ends, you have the minimum!

Comments

0

TRY this:

int min = arr[0];
    for(int j=1;j<arr.length;j++)
    {
            if(min>arr[j])
            {
                min= arr[j];
            }
    }
    System.out.println("min no is "+min);

Comments

0
      int min=0; 
        for (int i = 0; i < array.length; i++) {
        if (min > array[i]) {
        min = array[i];
        }
}

    System.out.println(min);

simple way to get MAX and MIN

To get MIN

System.out.println(getMinValue(your array)); 

and for MAX

System.out.println(getMaxValue(your array));

Comments

0

I am entering 5 values and storing them in the array and looping through them to find the min value but it prints 0 all the time. What's wrong in this code? I used the same logic for the max value and has no problem.

public class FindMinValue {

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        int i;
        int NUM_VALUES = 5;
        int[] userVals = new int [NUM_VALUES];
        
        System.out.println("Enter " + NUM_VALUES + " values one at a time.");
        
        int minVal = userVals[0]; //min value so far
        
        for(i = 0; i < userVals.length; ++i) {
            userVals[i] = scnr.nextInt();
            if(userVals[i] < minVal) {
                minVal = userVals[i];
            }
        }
        System.out.println("Min Value is :" + minVal);
        

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.