>I have to sort even and odd input numbers of an array, placing the even numbers at the front of the array, and the odd at the end.
Code:
#include <stdio.h>
#include <stdlib.h>
int eo ( const void *a, const void *b )
{
int ia = *(int *)a;
int ib = *(int *)b;
if ( ia % 2 == 0 && ib % 2 != 0 )
return -1;
else if ( ia % 2 != 0 && ib % 2 == 0 )
return +1;
else
return 0;
}
int main ( void )
{
int a[] = {1,2,3,4,5,6,7,8,9,0};
int i;
for ( i = 0; i < 10; i++ )
printf ( "%d ", a[i] );
printf ( "\n" );
qsort ( a, 10, sizeof ( int ), eo );
for ( i = 0; i < 10; i++ )
printf ( "%d ", a[i] );
printf ( "\n" );
return 0;
}
Replace qsort with your favorite sorting algorithm if you aren't familiar with it or aren't allowed to use it.