Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

How can I find the smallest value in a int array without changing the array order?

code snippet:

    int[] tenIntArray = new int [10];
    int i, userIn;

    Scanner KyBdIn = new Scanner(System.in);
    System.out.println("Please enter 10 integer numbers ");

    for(i = 0; i < tenIntArray.length; i++){
        System.out.println("Please enter integer " + i);
        userIn = KyBdIn.nextInt();
        tenIntArray[i] = userIn;
    }

I am not sure how I can find the smallest array value in the tenIntArray and display the position

For example the array holds - [50, 8, 2, 3, 1, 9, 8, 7 ,54, 10]

The output should say "The smallest value is 1 at position 5 in array"

share|improve this question
    
2  
ouch. Index counting starts at 0, not 1. As a condition you should use i < tenIntArray.length and not !=, just to be safe when you start using doubles or floats. – extraneon Dec 14 '13 at 16:51
    
Thanks for you help :D – Giovanni Lenguito Dec 14 '13 at 17:01
    
@extraneon It's not "just to be safe", it's the more logical, generally accepted way of doing things. – Dukeling Dec 14 '13 at 17:08
up vote 14 down vote accepted

This figure should be helpful :

enter image description here

Then to answer your question, what would you do on paper ?

  1. Create and initialize the min value at tenIntArray[0]
  2. Create a variable to hold the index of the min value in the array and initialize it to 0 (because we said in 1. to initialize the min at tenIntArray[0])
  3. Loop through the elements of your array
  4. If you find an element inferior than the current min, update the minimum value with this element and update the index with the corresponding index of this element
  5. You're done

Writing the algorithm should be straightforward now.

share|improve this answer

Try this:

//Let arr is your array of integers
if (arr.length == 0)
    return;
int small = arr[0];
int index = 0;
for (int i = 0; i < arr.length; i++)
    if (arr[i] < small)
    {
        small = arr[i];
        index = i;
    }
share|improve this answer
    
Thank you for your help! – Giovanni Lenguito Dec 18 '13 at 14:55

The method I am proposing will find both min and max.

public static void main(String[] args) {
    findMinMax(new int[] {10,40,50,20,69,37});
}
public static void findMinMax(int[] array) {
    if (array == null || array.length < 1)
        return;
    int min = array[0];
    int max = array[0];

    for (int i = 1; i <= array.length - 1; i++) {
        if (max < array[i]) {
            max = array[i];
        }

        if (min > array[i]) {
            min = array[i];
        }
    }
    System.out.println("min: " + min + "\nmax: " + max);
}

Obviously this is not going to one of the most optimized solution but it will work for you. It uses simple comparison to track min and max values. Output is:

min: 10 max: 69

share|improve this answer
    int[] input = {12,9,33,14,5,4};

    int max = 0;
    int index = 0;
    int indexOne = 0; 
    int min = input[0];
    for(int i = 0;i<input.length;i++)
    {
        if(max<input[i])
        {
            max = input[i];
            indexOne = i;
        }

        if(min>input[i])
        {
            min = input[i];
            index = i;
        }

    }
    System.out.println(max);
    System.out.println(indexOne);
    System.out.println(min);
    System.out.println(index);
share|improve this answer

Here is the function

public int getIndexOfMin(ArrayList<Integer> arr){
    int minVal = arr.get(0); // take first as minVal
    int indexOfMin = -1; //returns -1 if all elements are equal
    for (int i = 0; i < arr.size(); i++) {
        //if current is less then minVal
        if(arr.get(i) < minVal ){
            minVal = arr.get(i); // put it in minVal
            indexOfMin = i; // put index of current min
        }
    }
    return indexOfMin;  
}
share|improve this answer

the first index of a array is zero. not one.

for(i = 0; i < tenIntArray.length; i++)

so correct this. the code that you asked is :

    int small = Integer.MAX_VALUE;
    int i = 0;
    int index = 0;
    for(int j : tenIntArray){
        if(j < small){
            small = j;
            i++;
            index = i;
        }
    }
    System.out.print("The smallest value is"+small+"at position"+ index +"in array");
share|improve this answer
    
Your algorithm is wrong. Did you try it ? – Alexis C. Dec 14 '13 at 20:57
    
Yes I did. can you tell me about the fault? – user3101937 Dec 15 '13 at 5:18
1  
It's find the smallest but not the correct index. For example with the input int [] tenIntArray = {1,-8,2,2,0,-15};, you will increment the index variable 3 times. But that won't give you the correct index of the minimum value in the array. You have to keep track of the current index, and update another variable when you have found a smallest value (like you did for small). – Alexis C. Dec 15 '13 at 9:21
    
ah I got that.thanks.if I start with int index = -1; , is that correct? – user3101937 Dec 16 '13 at 5:24
    
No, that's not. – Alexis C. Dec 16 '13 at 8:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.