-->
当前位置:首页 > 题库 > 正文内容

程序填空题:QuickSelect

Luz4年前 (2021-05-10)题库964
The function is to find the `K`-th largest element in a list `A` of `N` elements. The initial function call is `Qselect(A, K, 0, N-1)`. Please complete the following program.

```c++
ElementType Qselect( ElementType A[], int K, int Left, int Right )
{
ElementType Pivot = A[Left];
int L = Left, R = Right+1;

while (1) {
while ( A[++L] > Pivot ) ;
@@[while ( A[--R] < Pivot )](3);
if ( L < R ) Swap( &A[L], &A[R] );
else break;
}
Swap( &A[Left], &A[R] );
if ( K < (L-Left) )
return Qselect(A, K, Left, R-1);
else if ( K > (L-Left) )
@@[return Qselect(A, K-(L-Left), L, Right)](3);
else
return Pivot;
}
```





答案:
第1空:while ( A[--R] < Pivot )

第2空:return Qselect(A, K-(L-Left), L, Right)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。