문제

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