You can use any sorting algorithm with the right comparison:
#include <stdio.h>
#include <stdlib.h>
int compare(void* const a, void* const b)
{
int const ia = *(int const*)a;
int const ib = *(int const*)b;
return (abs(ia) % 2) - (abs(ib) % 2);
}
int main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
size_t const sz = sizeof a / sizeof *a;
size_t x;
qsort(a, sz, sizeof *a, compare);
for (x = 0; x < sz; ++x) printf("%d%s", a[x], x < sz - 1 ? " " : "\n");
return 0;
} It does not matter that I used qsort. The comparison function is the part that matters for this example. Another way to do it is walk down the array checking for evens, and if the value is not even, move it to the end. The only hard part to that method is filling in the hold left by the vacated value:
/* fill in the hold at &a[x] */
memmove(&a[x], &a[x+1], (SIZE - x) * sizeof *a);
/* now a[SIZE-1] is open for */ You can do the same thing with a loop by shifting every value after the vacated value one spot to the left:
/* fill in the hold at &a[x] */
for (y = x; y < SIZE - 1; ++y) a[y] = a[y+1];
/* now a[SIZE-1] is open for */