0

I have an array filled with double values. I want to get the index of the 2nd and the 3rd and 4th lowest values in the array. This is what I have tried but I haven't been getting what I want.

double[] wantedVals = new double [3];
    for(double n :allValues){
        if(n < wantedVals[2] && n > wantedVals[1]) {
            wantedVals[2] = n;
        }
    }
    System.out.println("All Values: "+ Arrays.toString(allValues));
    System.out.println("2nd, 3rd, 4th lowest values index: " + Arrays.toString(wantedVals));

}

Here is the output.

All Values: [314.8490027477457, 558.1219589782775, 0.0, 538.3207360335937, 519.5707513178547, 271.85862019623363, 452.44377672120766, 448.3506884613316, 365.0024775172766, 380.61611237571117, 225.73376879634114, 310.28009020077326, 121.53621181051349, 95.45317487517113, 280.453364828718, 57.11775118122211, 343.1257001358977, 365.58868943629807, 530.7625668260243, 227.4473840254049, 319.9578951791938, 291.8377585984206, 494.999842171692, 464.97103404405743]
2nd, 3rd, 4th lowest values: [0.0, 0.0, 0.0]

What I want to do is get the indexes of the 2nd, 3rd and 4th lowest values in All Values array.

5
  • Is it an option to sort (and therefore re-arrange) the arrays? Because that would probably be the easiest route. Commented Apr 14, 2017 at 0:00
  • Why do you want the indexes? Are you sure you don't want the 2nd, 3rd and 4th lowest values? Commented Apr 14, 2017 at 0:00
  • 1
    wantedVals[2] is initially 0. None of your values are negative, so n < wantedVals[2] (aka n < 0) is always false, so nothing is ever assigned to wantedVals[2], and hence the array is all zeroes. --- FYI: If you need to know the index of a value in allValues, you can't use the enhanced for-loop. You must use an regular for-loop. Commented Apr 14, 2017 at 0:00
  • @Bohemian no i need the index because later on I use it to change the values of another array Commented Apr 14, 2017 at 0:29
  • Oh ok I will change that @Andreas Commented Apr 14, 2017 at 0:33

2 Answers 2

1

Do an index sort, and then output the indexes. Like this:

final int MAX_ARRAY_SIZE = (the biggest that your all values array will be)
int index[] = new int[MAX_ARRAY_SIZE];
for(int i = 0; i < allValues.length(); i++)
{
    index[ i ] = i;
}
//Simple Bubble Sort
for(int i = 0; i < allValues.length() - 1; i++)
{
    for(int j = i + 1; j < allValues.length(); j++)
    {
        if(allValues[ index[ i ] ] > allValues[ index[ j ] ])
        {
            int temp = index[ i ];
            index[ i ] = index[ j ];
            index[ j ] = temp;
        }
    }
}
System.out.println("Indexes of 2nd to 4th lowest numbers are the following: ");
for(int i = 1; i < 4; i++)
{
    System.out.println(index[ i ]);
}

Mark this as the answer if it answers your question.

Hope this helps!

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

7 Comments

do I have to sort the array? The original indexes in the allValues array are important and sorting it would mess up the index that those specific values I want index.
No it wouldn't. If you actually looked at the code, you would see that the indexes array is being sorted, not your actual data.
How did you figure out the way to solve this problem, I want to be able to become better at solving my own problems so that I don't have to post on questions I get stumped on.
Practice, Practice, Practice. I code for at least 3-4 hours a day. The key to learning is failing. I've struggled with programs for literally weeks before I have found my stupid little mistake, and guess what, I never make the same mistake again.
Wow, I program several hours a day probably around 2-3. I have been programming for 2 years and recently started to have an easier figuring out how to break big problems into smaller ones. Thanks again for the help :)
|
1

I can think of two ways to accomplish this: sort the array, and search it repetitively. Neither of these is likely to be the most efficient, but they should be good enough for most use cases (with the latter being slightly more efficient).

If you want to sort the array, simple use Arrays.paralellSort() and get whatever values you need from the resulting array.

If you want to search it repetitively, then simply go through the array and find the lowest value. Make note of this value in a variable, and repeat the original code -- unless the current value is the lowest. It would be best to use a HashSet to contain the list of already selected values, since a HashSet can efficiently be checked for a value and have a value added to it.

2 Comments

But the question states OP wants the indexes, not the values.
@Bohemian In the second solution, the HashSet stores the indecies. For the fist solution, it is easy enough to just linear-search for the matching value in the original array.

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.