简体   繁体   English

中位数选择算法的中位数

[英]Median of medians select algorithm

for the last few days I've been trying really hard to write this algorithm but without success.在过去的几天里,我一直在努力编写这个算法,但没有成功。 The code works and most of the time it gives me the right result but there are some cases where it fails.该代码有效,并且大多数时候它给了我正确的结果,但在某些情况下它会失败。 For example with this array {3, 8, 1, 9, 10, 7, 6, 2, 5, 4} and k = 6 it should give me 6 as result but it gives me 7. Can someone help me?例如,对于这个数组 {3, 8, 1, 9, 10, 7, 6, 2, 5, 4} 和 k = 6 它应该给我 6 作为结果,但它给我 7。有人可以帮助我吗? I can't figure out what's the problem.我无法弄清楚是什么问题。

Here is the code:这是代码:

class MOMSelect {

static int median(int a[], int i, int n) {
    if(i <= n)
        Arrays.sort(a, i, n);
    else
        Arrays.sort(a, n, i);
    return a[n/2];
}

static int medianOfMediansSelect(int a[], int left, int right, int k) {
    int n = right - left + 1;
    int i;
    int[] medians = new int[(n + 4) / 5];
    for (i = 0; i < n/5; i++) {
        medians[i] = median(a, left + i * 5, 5);
    }
    if (i*5 < n) {
        medians[i] = median(a,left + i * 5, n % 5);
        i++;
    }
    int medianOfMedians = (i == 1)? medians[i - 1]: median(medians, 0, medians.length);
    int pivotIndex = partition(a, left, right, medianOfMedians);
    if (pivotIndex == k - 1) {
        return a[pivotIndex];
    }
    else if (pivotIndex - left > k - 1) {
        return medianOfMediansSelect(a, left, pivotIndex - 1, k);
    }
    else {
        return medianOfMediansSelect(a, pivotIndex + 1, right, k);
    }
}

static void swap(int[] a, int i, int j) {
    int temp = a[i];
    a[i] = a[j];
    a[j] = temp;
}

static int partition(int[] a, int left, int right, int x) {
    int i;
    for (i = left; i < right; i++)
        if (a[i] == x)
            break;
    swap(a, i, right);
    i = left;
    for (int j = left; j <= right - 1; j++) {
        if(a[j] <= x) {
            swap(a, i, j);
            i++;
        }
    }
    swap(a, i, right);
    return i;
}

public static void main(String[] args) {
    int a[] = {3, 8, 1, 9, 10, 7, 6, 2, 5, 4};
    int n = a.length;
    int k = 1;
    System.out.println(medianOfMediansSelect(a, 0, n - 1, k));
}
}

Thanks in advance to everyone提前感谢大家

Ok I solved.好的我解决了。 Further than my bad understanding of the Arrays.sort() method there was a stupid mistake on the if stucture where I check the pivotPosition value on the method medianOfMediansSelect()而且比我的Arrays.sort()方法的不良理解有上如果stucture一个愚蠢的错误,我检查的方法pivotPosition值medianOfMediansSelect()

More precisely on this line更准确地说,在这条线上

else if (pivotIndex - left > k - 1) {

I should have done like this我应该这样做

else if (pivotIndex > k - 1) {

So, I modified your median method to correct the problem.所以,我修改了你的median方法来纠正这个问题。 You should check Arrays.sort method for a clear understanding.您应该检查Arrays.sort方法以获得清晰的理解。

Now it gives 6 , for k=6 .现在它给出6 ,对于k=6

Here is it,就这个,

   static int median(int a[], int i, int n)
   {
      Arrays.sort(a, i, i + n - 1);

      return a[i + n / 2];
   }

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

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