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

程序填空题:None-recursive Quick Selection algorithm

Luz4年前 (2021-05-10)题库743
None-recursive Quick Selection algorithm

The function is to find the `K`-th smallest element in a list `A` of `N` elements. The initial function call is `QSelect(A, N, K)`. Please complete the following program.

```c++
ElementType QSelect( ElementType A[], int N, int K )
{
ElementType Pivot;
int L, R, Left, Right, K1;
Left = 0; Right = N-1; K1 = K;
for(;;) {
L = Left, R = Right+1;
Pivot = A[Left];
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 ( K1 < (L-Left) )
Right = R-1;
else if ( K1 > (L-Left) ){
@@[K1 = K1-(L-Left)](3);
Left = L;
}else
return Pivot;
}
}

```





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

第2空:K1 = K1-(L-Left)

发表评论

访客

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