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

程序填空题:FindKthLargest

Luz4年前 (2021-05-10)题库1324
The function is to find the `K`-th largest element in a list `A` of `N` elements. The function `BuildMinHeap(H, K)` is to arrange elements `H[1]` ... `H[K]` into a min-heap. Please complete the following program.

```c++
ElementType FindKthLargest ( int A[], int N, int K )
{ /* it is assumed that K<=N */
ElementType *H;
int i, next, child;

H = (ElementType *)malloc((K+1)*sizeof(ElementType));
for ( i=1; i<=K; i++ ) H[i] = A[i-1];
BuildMinHeap(H, K);

for ( next=K; next H[0] = A[next];
if ( H[0] > H[1] ) {
for ( i=1; i*2<=K; i=child ) {
child = i*2;
if ( child!=K && @@[H[child+1] if ( @@[H[0]>H[child]](3) )
H[i] = H[child];
else break;
}
H[i] = H[0];
}
}
return H[1];
}

```





答案:
第1空:H[child+1]
第2空:H[0]>H[child]

发表评论

访客

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