简体   繁体   English

返回未排序列表的第n个最高值的索引

[英]Returning the index of the n-th highest value of an unsorted list

I have written the following code and am now trying to figure out the best way to achieve what is explained in the 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 an elegant way o doing so? 这样做会有什么优雅的方式? I do not need to keep expected values as an array - I am happy to use any data structure. 我不需要将期望值保持为数组 - 我很乐意使用任何数据结构。

You could create a new array containing the indices and sort on the values - in semi-pseudo code it could look like this (to be adapted): 您可以创建一个包含索引的新数组并对值进行排序 - 在半伪代码中它可能看起来像这样(需要调整):

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];

If you want to work with simple arrays, maybe this might be a 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;
}

You can "wipe out" the highest value n-1 times. 您可以“消灭”最高值n-1次。 After this the highest value is the n-th highest value of the original array: 在此之后,最高值是原始数组的第n个最高值:

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;
}

The complexity is O(n * numbers.length) . 复杂度为O(n * numbers.length)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM