Returning the index of the nth highest value of an unsorted list

I wrote the following code and now I am trying to find the best way to achieve what is explained in four comments:

    Integer[] expectedValues = new Integer[4];

    for (int i = 0; i <= 3; i++) {
        expectedValues[i] = getExpectedValue(i);
    }

    int choice = randomNumGenerator.nextInt(100) + 1;
    if (choice <= intelligence) {
        // return index of highest value in expectedValues
    } else if (choice <= intelligence * 2) {
        // return index of 2nd highest value in expectedValues
    } else if (choice <= intelligence * 3) {
        // return index of 3rd highest value in expectedValues
    } else {
        // return index of lowest value in expectedValues
    }

      

What would be the elegant way? I don't need to store the expected values ​​as an array - I'm happy to use any data structure.

+3


source to share


3 answers


You can create a new array containing the indices and sort by value - in semi-pseudocode it might look like this (for adaptation):



int[][] valueAndIndex = new int[n][2];

//fill array:
valueAndIndex[i][0] = i;
valueAndIndex[i][1] = expectedValues[i];

//sort on values in descending order
Arrays.sort(valueAndIndex, (a, b) -> Integer.compare(b[1], a[1]));

//find n-th index
int n = 3; //3rd largest number
int index = valueAndIndex[n - 1][0];

      

+1


source


If you want to work with simple arrays, maybe this could be the solution:



public static void main(String[] args) {
    int[] arr = new int[] { 1, 4, 2, 3 };
    int[] sorted = sortedCopy(arr);

    int choice = randomNumGenerator.nextInt(100) + 1;
    if (choice <= intelligence) {
        System.out.println(findIndex(arr, sorted[3])); // 1
    } else if (choice <= intelligence * 2) {
        System.out.println(findIndex(arr, sorted[2])); // 3
    } else if (choice <= intelligence * 3) {
        System.out.println(findIndex(arr, sorted[1])); // 2
    } else {
        System.out.println(findIndex(arr, sorted[0])); // 0
    }
}

static int[] sortedCopy(int[] arr) {
    int[] copy = new int[arr.length];
    System.arraycopy(arr, 0, copy, 0, arr.length);
    Arrays.sort(copy);
    return copy;
}

static int findIndex(int[] arr, int val) {
    int index = -1;
    for (int i = 0; i < arr.length; ++i) {
        if (arr[i] == val) {
            index = i;
            break;
        }
    }
    return index;
}

      

0


source


You can "erase" the maximum value n-1 times. After that, the highest value is the nth highest value of the original array:

public static void main(String[] args) {
    int[] numbers = new int[]{5, 9, 1, 4};
    int n = 2; // n-th index

    for (int i = 0; i < n - 1; ++i) {
        int maxIndex = findMaxIndex(numbers);
        numbers[maxIndex] = Integer.MIN_VALUE;
    }

    int maxIndex = findMaxIndex(numbers);
    System.out.println(maxIndex + " -> " + numbers[maxIndex]);
}

public static int findMaxIndex(int[] numbers) {
    int maxIndex = 0;
    for (int j = 1; j < numbers.length; ++j) {
        if (numbers[j] > numbers[maxIndex]) {
            maxIndex = j;
        }
    }
    return maxIndex;
}

      

Complexity O(n * numbers.length)

.

0


source







All Articles