程序填空题:Modified Selection Sort
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
```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( @@[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