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);
}
Chewy
  • 85
  • 2
  • 4
  • 15
  • 1
    What is the value of `max`? Is it equal to `arr.length`? – rgettman Feb 17 '15 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) – Barranka Feb 17 '15 at 19:08
  • yes, max is equal to arr.length. I could using arr.length instead and see if that makes a difference – Chewy Feb 17 '15 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 –  Feb 17 '15 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. – gran_profaci Feb 17 '15 at 19:16
  • Also, this is covered in http://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-using-java – gran_profaci Feb 17 '15 at 19:17
  • So I edited my code to use nElems which in essence is the number of elements I have in it. This is because whenever I call my insert method to insert an element I increment that variable .. Verified that the change resulted in a positive result – Chewy Feb 17 '15 at 19:19
  • You shouldn't have your insert / delete code in the same loop as the one being iterated over. – gran_profaci Feb 17 '15 at 19:25
  • You sample does not compile, but if it did. You need to initialize the min element with something big so all numbers in array are for sure smaller. if you initiialize it with 0, none of them would be. – eckes Feb 18 '15 at 01:22

3 Answers3

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.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
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();
Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
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;
}
gran_profaci
  • 8,087
  • 15
  • 66
  • 99