0

I'm trying to find the smallest integer in an ArrayList just by using two simple for loops. I tried originally with one for loop, but it wasn't updating correctly. I haven't learned collections yet, so this should be done without using any collections code.

This is what I have:

public static void printInOrder(ArrayList<Integer> data){

 int minIndex = 0 ;
 for(int i = 0; i < data.size(); i++){
   minIndex = i;
   for(int j = i + 1; j < data.size() - 1; j++){
     if(data.get(j) < data.get(minIndex)){
       minIndex = j;}

   }
   System.out.println(data.get(minIndex) + " ");
 }
}//printInOrder

My minimum always seems to be the last value in the list, so I tried printing the data.get(minIndex) in the first for loop to see what happens, and it seems to update it different values, and then finally, the last value. I have no idea why this is happening.

This is what it's printing for example:

Original list: [47, 19, 46, 42, 15, 26, 36, 27, 13, 15, 1, 40, 34, 14, 6, 34, 28, 12, 15, 13] Print the minimum: 1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 12 12 12 15 13

8
  • 1
    You don't need double loop.. A single loop should do it as long as you are keeping the current minimum Commented Apr 9, 2015 at 18:51
  • Sort the arraylist and return the element at index 0? Commented Apr 9, 2015 at 18:52
  • @gtgaxiola, if I tried it with a single loop, what would I compare in my if statement? Because I tried that way for a while, but it too wasn't giving me the correct minimum. Commented Apr 9, 2015 at 18:55
  • You keep a temporary variable to store the current minimum. Commented Apr 9, 2015 at 18:56
  • 1
    Your code currently successfully finds the smallest number of the list starting at i, for all starting indices i. You wanted to find the smallest number of the list starting at 0 only. Commented Apr 9, 2015 at 19:00

3 Answers 3

3

You can do this quick solution if you want to avoid using Collections.

public static void printInOrder(ArrayList<Integer> data){

    Integer[] array = (Integer[]) data.toArray();
    Arrays.sort(array);
    System.out.println(array[0]);

}

The smallest integer would always be at index 0 in this case.

Or, if you do want to go through the whole ArrayList using a loop, I'd suggest you store the actual value in a variable instead of the index. Doing so, you get:

public static void printInOrder(ArrayList<Integer> data){

    int minInteger = data.get(0);
    for(int i = 1; i < data.size(); i++){
        if(data.get(i) < minInteger) minInteger= data.get(i);
    }
    System.out.println(minInteger);

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

2 Comments

Integer[] array = data.toArray(); doesn't compile.
My bad, forgot to cast. :)
0

You're not supposed to do sorting to find the minimum

Here's the code

public static void Min(int arr[])
{
   int min = arr[0];
    int i=0;
while(i<arr.length)
 {
        if (arr[i] < min) {
            min=arr[i];
        }
    }
    return min;
}

3 Comments

Well , if you are interested in sorting and then find the minimum no problem but the issue was it didn't make a sense and i think it takes up double time to find you a minimum with a single min algo
Well by analysis, a linear search would have a complexity of O(n), having n iterations worst-case. Using Java's built-in sorting function, it runs at O(n log n), and getting the minimum after that would be O(1) for sure. This, way, I don't think it's as inefficient as it may seem.
@theguywhodreams Thats one way to look at it, the other is that log2(1024) already works out to 10, so sorting 1024 elements to find the minimum has approximately 10 times the complexity of the linear search. If that is to be considered efficient (enough) or not is highly dependend on the actual use case.
0

Untested:

public static int calculateMinIndex(final ArrayList<Integer> data) {
    int minIndex = 0;
    for(int i = 0; i < data.size() - 1; i++) {
        if(data.get(i) > data.get(i+1)) {
            minIndex = i+1;
        }
    }
    return minIndex;
}

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.