3,881
社区成员




#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[] = { 1, 5, 3, 7, 4, 2, 6 };
int b[100];
int size = sizeof( a ) / sizeof( a[0] );
int mid = size / 2;
nth_element( a, a+mid, a+size );
int index1, index2, index3 = 0;
for( index1 = 0, index2 = mid+1; index1 < mid; ++index1, ++index2 )
{
b[index3++] = a[index1];
if( index2 < size )
b[index3++] = a[index2];
}
b[index3] = a[mid];
for( int i1 = 0; i1 < size; ++i1 )
{
cout << b[i1] << " ";
}
return 0;
}