1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#include <iostream>
using namespace std;
void partition( int n, int *A )
{
int numOdd = 0;
for ( int *a = A; a != A + n; a++ ) numOdd += *a % 2 == 1;
if ( numOdd == 0 || numOdd == n ) return;
int *Odd = new int[numOdd];
for ( int *o = Odd + numOdd - 1, *a = A + n - 1, *e = a; a != A - 1; a-- )
{
if ( *a % 2 ) *o-- = *a;
else *e-- = *a;
}
for ( int *a = A, *o = Odd; o != Odd + numOdd; ) *a++ = *o++;
delete [] Odd ;
}
//==========================================================
void print( int n, int *A )
{
for ( int *p = A; p != A + n; p++ ) cout << *p << ' ';
cout << '\n';
}
//==========================================================
int main()
{
int A[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int N = sizeof A / sizeof A[0];
print( N, A );
partition( N, A );
print( N, A );
}
|