小编Emm*_*iay的帖子

不了解中位数算法的中位数来找到第k个元素

下面是我试图理解中位数算法的中位数的代码(使用大小为5的块).我理解如何获得输入的中位数,但我不知道如何编码块以保持递归输入,直到我只有中位数.在获得该中位数之后,我不确定如何使用它作为枢轴来丢弃无用的信息来分区输入.getMediansArray返回一个大小为ceil(input.length/5)getMedians的数组,只返回数组的中位数(仅用于长度<= 5的数组).

public static int[] findKthElement(int[] input, int k) {
    int numOfMedians = (int) Math.ceil(input.length/5.0);
    int[] medians = new int[numOfMedians];
    medians = getMediansArray(input, medians)

    // (1) This only gets the first iteration of medians of the
    // input. How do I recurse on this until I just have one median?

    // (2) how should I partition about the pivot once I get it?
}

public static int[] getMediansArray(int[] input, int[] medians) {
    int numOfMedians = (int) …
Run Code Online (Sandbox Code Playgroud)

java arrays algorithm median-of-medians

7
推荐指数
1
解决办法
1099
查看次数

标签 统计

algorithm ×1

arrays ×1

java ×1

median-of-medians ×1