Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get lowest 3 elements in an int array

Hi I want to get the lowest 3 elements of an array. By lowest I mean the minimum value. I cannot use the collections.Sort method as I need to know the index of the elements. Therefore I am using the following code to get the lowest, but I need to know how I can get the lowest 3.

int minimum = grades[1];
int index = 1;

for(i=1; i<= numberOfStudents; i++){
    if (grades[i]<minimum){
        minimum = grades[i];
        index = i;
    }
}
like image 860
Matt9Atkins Avatar asked Dec 02 '12 22:12

Matt9Atkins


People also ask

What does Arr MIN () do?

The function getresult( int arr[],int n) is to find the maximum and minimum element present in the array in minimum no. of comparisons. If there is only one element then we will initialize the variables max and min with arr[0] . For more than one element, we will initialize max with arr[1] and min with arr[0].


2 Answers

Here is a really simple way of doing it:

public static void main(String[] args) {
    int[] myArray = { 5, 8, 12, 9, 50, 11, 4 };

    System.out.println(Arrays.toString(myArray));
    System.out.println(Arrays.toString(getThreeLowest(myArray)));
}

private static int[] getThreeLowest(int[] array) {
    int[] lowestValues = new int[3];
    Arrays.fill(lowestValues, Integer.MAX_VALUE);

    for(int n : array) {
        if(n < lowestValues[2]) {
            lowestValues[2] = n;
            Arrays.sort(lowestValues);
        }
    }
    return lowestValues;
}

This outputs:

[5, 8, 12, 9, 50, 11, 4]
[4, 5, 8]


The call to Arrays.sort is only done to the local array, not your main array. The reason it does this is just to simplify the comparison against n.

like image 92
Phil K Avatar answered Oct 14 '22 16:10

Phil K


Building off what you had

    int[] grades = { 100, 99, 98, 97, 10, 95, 11, 9, 94 };
    int numberOfStudents = grades.length;

    int minimum = grades[1];
    int minimum2 = grades[1];
    int minimum3 = grades[1];
    int index = 1;
    int index2 = 1;
    int index3 = 1;

    for(int i=1; i< numberOfStudents; i++){
        if (grades[i]<minimum3 && grades[i]>=minimum2){
            minimum3 = grades[i];
            index3 = i;
        }
        if (grades[i]<minimum2 && grades[i]>=minimum){
            //We have a new 2nd lowest - shift previous 2nd lowest up
            minimum3 = minimum2;
            index3 = index2;
            minimum2 = grades[i];
            index2 = i;
        }
        if (grades[i]<minimum){
            //We have a new lowest - shift previous lowest up
            minimum3 = minimum2;
            index3 = index2;
            minimum2 = minimum;
            index2 = index;
            minimum = grades[i];
            index = i;
        }
    }
    System.out.println("Smallest is at " + index + " with value of " + minimum);
    System.out.println("Next Smallest is at " + index2 + " with value of " + minimum2);
    System.out.println("Next Smallest is at " + index3 + " with value of " + minimum3);
like image 27
Tad Avatar answered Oct 14 '22 17:10

Tad