سؤال

I am trying to use quickselect in c++ to do this, but it keeps returning me the kth smallest element instead of the kth largest. Where is my logic wrong?

int partition(int* input, int p, int r)
{
    int pivot = input[r];

    while ( p < r )
    {
        while ( input[p] < pivot )
            p++;

        while ( input[r] > pivot )
            r--;

        if ( input[p] == input[r] )
            p++;
        else if ( p < r ) {
            int tmp = input[p];
            input[p] = input[r];
            input[r] = tmp;
        }
    }

    return r;
}

int quick_select(int* input, int p, int r, int k)
{
    if ( p == r ) return input[p];
    int j = partition(input, p, r);
    int length = j - p + 1;
    if ( length == k ) return input[j];
    else if ( k < length ) return quick_select(input, p, j - 1, k);
    else  return quick_select(input, j + 1, r, k - length);
}

What should I change to make this kth largest instead of kth smallest element?

هل كانت مفيدة؟

المحلول

the < and > in your code is reverse in partition() as @Dietmar Kühl mentioned,by change them,it works correctly.

besides,my suggestion is to use a normal partition() of quicksort as follow, for whose two index of which move in the same direction and one of them never surpasses another. It is not easy to make anyone confused.

int partition(int *input, int p, int r) {
    int pivot,i,j,tmp;
    pivot = input[r];
    i = p-1;
    for (j=p;j<=r-1;j++) {
        if (input[j]>= pivot) {
            i++;
        tmp = input[i];
        input[i] = input[j];
        input[j] = tmp;
        }
    }
    tmp = input[i+1];   
    input[i+1] = input[r];
    input[r] = tmp;
    return i+1;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top