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

程序填空题:Modified Selection Sort

Luz4年前 (2021-05-10)题库644
The function is to sort the list { `r[1] … r[n]` } in non-decreasing order. Unlike selection sort which places only the minimum unsorted element in its correct position, this algorithm finds both the minimum and the maximum unsorted elements and places them into their final positions.

```c++
void sort( list r[], int n )
{
int i, j, mini, maxi;

for (i=1; i mini = maxi = i;
for( j=i+1; @@[j<=n-i+1](3); ++j ){
if( @@[r[j]->key < r[mini]->key](3) ) mini = j;
else if(r[j]->key > r[maxi]->key) maxi = j;
}
if( @@[mini != i](3) ) swap(&r[mini], &r[i]);
if( maxi != n-i+1 ){
if( @@[maxi == i](3) ) swap(&r[mini], &r[n-i+1]);
else swap(&r[maxi], &r[n-i+1]);
}
}
}
```





答案:
第1空:j<=n-i+1

第2空:r[j]->key < r[mini]->key

第3空:mini != i

第4空:maxi == i

发表评论

访客

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