0

Testing out code to find the min and max element in an array. My code for finding the maximum works fine .. but my min prints "Minimum element in the array: 0". I probably have one little mistake that is throwing me off lol.

 public void deleteMin() {

  // set value for comparision starting from the beginning of the array
  int arrayMin = arr[0];

  for (int j = 0; j < nElems; j++) {
     if (arr[j] < arrayMin) {
        arrayMin = arr[j];
     }
    // insert delete functionality    
  } 
  System.out.println("Minimum element in the array: " + arrayMin);
}
9
  • 1
    What is the value of max? Is it equal to arr.length? Commented Feb 17, 2015 at 19:08
  • Doesn't look wrong to me... Have you tried debugging your code (step-by-step execution can help you see what's going on) Commented Feb 17, 2015 at 19:08
  • yes, max is equal to arr.length. I could using arr.length instead and see if that makes a difference Commented Feb 17, 2015 at 19:09
  • You should verify that there's an element 0, also if there is one, the for-loop can start at index 1 Commented Feb 17, 2015 at 19:14
  • Instead of asking this, you should insert a print statement in the loop there and display the output. That would help you far greater than over here. Commented Feb 17, 2015 at 19:16

3 Answers 3

10

Your code is correct as it stands now. The only reason for arrayMin to contain 0 after the code has finished is if

  • nElems was not set to arr.length, or
  • 0 was indeed the smallest element in the array.

The code could be simplified.

Java 8:

int arrayMin = IntStream.of(arr).min().getAsInt();

Java 7:

int arrayMin = arr[0];
for (int i : arr)
    arrayMin = Math.min(arrayMin, i);

(Note that both solutions assume a non-empty array since min would otherwise be undefined.)

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

Comments

1

You could do

List<Integer> yourList = new ArrayList<Integer>(arr.length);
for (int i= 0; i< arr.length; i++)
    yourList.add(arr[i]); 

Collections.min(yourList);
Collections.max(yourList);

See max and min documentation.

Or if you have Java 8 as @ioobe already mentionned :

IntStream.of(arr).min().getAsInt();

1 Comment

Unfortunately, I can't use array lists .. has to be a regular array set
0

If you're just iterating over the array, then utilize the fact that you can have two pointers going instead of just the one.

Hence, this can get the job done with half the number of iterations :

public static int minElementInArray(int[] a) {      
    int minArrayElement = Integer.MAX_VALUE;

    for (int i = 0; i < a.length / 2; i++) {
        if (a[i] < minArrayElement) {
            minArrayElement = a[i];
        } 

        if (a[a.length - i - 1] < minArrayElement) {
            minArrayElement = a[a.length - i - 1];
        }
    }       
    return minArrayElement;
}

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.