DescriptionNote: You only need to commit the code that is marked as modified in the section, please follow the C + + language.
Binary selection sequencing: the traditional selection sorting algorithm improved, in a trip comparison process, simultaneously record the maximum and minimum position, the minimum value and the first element exchange, the maximum value and the last element exchange, that is, a trip to determine two elements, the remaining sequence repeats the above process, until the sequence is empty.
#include <iostream>
using namespace Std;
int main ()
{
int a[100],i,n;
cin>>n;
for (i=0; i<n; i++)
Cin>> A[i];
Int J, Minpos, Maxpos, TMP;
for (i=0; I <n/2; i++)
{
Minpos = i;
Maxpos = i;
for (j= i+1; j<= n-i-1; j + +)
{
if (A[j] > A[maxpos])
{
Maxpos = j;
Continue;
}
if (a[j]< A[minpos])
Minpos = j;
}
/***** Modified starting line ******/
TMP = A[i];
A[i] = A[minpos];
A[minpos] = tmp;
TMP = A[n-i];
A[n-i] = A[maxpos];
A[maxpos] = tmp;
/***** modified terminating line *****/
}
for (i = 0; i<n; i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}
Input
N and N integers
Output
From small to large sequence
Sample Input
8 12 35 69 84 52 54 75 65
Sample Output
12 35 52 54 65 69 75 84
HINT
The code is as follows:
#include <iostream>using namespace Std;int main () { int a[100],i,n; cin>>n; for (i=0; i<n; i++) cin>> a[i]; Int J, Minpos, Maxpos, tmp; for (i=0; I <n/2; i++) { minpos = i; Maxpos = i; for (j= i+1; j<= n-i-1; j + +) { if (A[j] > A[maxpos]) { maxpos = j; Continue; } if (a[j]< A[minpos]) minpos = j; } TMP = A[i]; A[i] = A[minpos]; A[minpos] = tmp; if (maxpos==i) { tmp = A[minpos]; A[minpos] = a[n-i-1]; A[N-I-1] = tmp; } else { tmp = A[maxpos]; A[maxpos] = a[n-i-1]; A[N-I-1] = tmp; } } for (i = 0; i<n; i++) cout<<a[i]<< ""; cout<<endl; return 0;}
Operation Result:
OJ the "Binary choice sort" of brush question