I am trying to get at least 5 minimum values from an array of integer my code attached works but it skips few values

public static void main(String[] args) {
    int array[] = {0, 1, 2, 1, 4, 5, 1, 7, 8, 1, 10, 11, 12, 13, 1, 15, 16, 17, 18, 19, 20, 2, 22, 23};

    int min = 0;
    int index = 0;
    String output = "";
    for (int x = 0; x < 5; x++){
        min = array[x];
        index++;
        for(int i = index, limit = array.length; i < limit; ++i){
            if(array[i] <= min){
                min = array[i];
                index = i + 1;
                break;
            }
        }
    output += index + "\t";
    }
    System.out.println(output);    
}

Recommended Answers

All 7 Replies

You're trying to grab the lowest five values in your array, right? What would really help you is if you commented your code, so you're forced to explain your thought process to yourself. That way, it will become more obvious why your code doesn't work.

The reason your program is "skipping values" is because, before you iterate through all the entries in the array with the loop for(int i = index, limit = array.length; i < limit; ++i), you're incrementing the starting index with the line index++;. Because i is initialzed to index, what's happening is that you're skipping the first element of your array the first time around, and then you're skipping the first and second elements of your array the second time around, and so on.

What purpose does the line min = array[x]; serve? You need to be asking these sorts of questions.

An idea for your solution... How about sort your array and then take values from the first (for ascending order) 5 indices from the sorted version?

i think you can go with sort() method of array.
It will sort your entire array into descending order, than first five values are the minimum value of whole array.

You can go with

Arrays.sort(array);

and then display array[0] to array[5] - first five minimum value.

I think you mean "ascending" - lowest values first

Also, you would like to print array[0] to array[4] to get the first 5 values :)

index = i + 1;

i believe this line is (in part) your culprit.

from a quick look the rest of the code makes sense although it might not be the most efficient way to find the 5 lowest values, but your inner loop starts at i = index, and lets say the first smallest value was in the position 500 of your array then the next time the inner loop is started the line i pointed will have index set to 501 and the loop will start from 501, thus skipping anything bellow.

Because i is initialzed to index, what's happening is that you're skipping the first element of your array the first time around, and then you're skipping the first and second elements of your array the second time around, and so on.

like Tumlee pointed out, the whole thing about starting your inner loop further is wrong because as you are finding lowest values you are not sorting the array, thus even if on the first go arround the number in position "2" isnt the SMALLEST value, it doesnt mean that it isnt the 4th smallest and starting from position 5 on round 4 would skip that number.

try completly removing index from your algortihm , make your inner loop go through all positions. Then figure a way to only skip the previously counted values in your subsequent loops. what i mean is , if theres three "1" in your array and two "2", these are your smalllest values, on your second run through the array, you must not count the first "1" that was counted as smallest value, but you want (or not depending how you see the problem) to count the other two "1"s in the array . What i would do if i wanted to count all three appearences of "1" in my array as three different smallest values, is i would keep a array of length 5 and store in there the indexes at which i found the smallest values each round. then on other rounds simply skip over these indexes while doing my search for the smallest value.

i hope this makes sense , let us know how you handle your issue :)

@james: thanks for correcting me.means ascending.
@NP: also thanks a lot for correcting in array[0] to array[4] to first five,instead i have written array[0] to array[5].

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.