程序填空题:Modified Selection Sort [2]
The function is to sort the list { `r[1] … r[n]` } in non-increasing order. Unlike selection sort which places only the maximum 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( maxi != i ) swap(&r[maxi], &r[i]);
if( mini != n-i+1 ){
if( @@[mini == i](3) ) swap(&r[maxi], &r[n-i+1]);
else swap(&r[mini], &r[n-i+1]);
}
}
}
```
答案:
第1空:j<=n-i+1
第2空:r[j]->key < r[mini]->key
第3空:mini == i
```c++
void sort( list r[], int n )
{
int i, j, mini, maxi;
for (i=1; 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( maxi != i ) swap(&r[maxi], &r[i]);
if( mini != n-i+1 ){
if( @@[mini == i](3) ) swap(&r[maxi], &r[n-i+1]);
else swap(&r[mini], &r[n-i+1]);
}
}
}
```
答案:
第1空:j<=n-i+1
第2空:r[j]->key < r[mini]->key
第3空:mini == i