# Thread: Sorting Arrays - Even and Odd Input

1. Originally posted by deedlit
Is it possible to add something to this program to make it so the latter half, the odd numbers, are also in the same order they began in at the end of the array?

Ex:

before: 1428754744
after: 4284441757

Thanks
Yes it's possible.

Quzah.

2. Since you're new to functions, a good example might be an exercise in code reuse. Here, we just use a generic 'copy_if' function to do all the work.

Code:
```#ifndef BOOL
typedef int BOOL;
#endif

int copy_if(
int destination[],
int * dest_length,
int source[],
int src_length,
BOOL (*criteria)(int)
)
{
int s, d = 0;

for(s = 0; s < src_length; ++s)
{
if( criteria(source[s]) )
{
destination[d++] = source[s];
}
}

if(dest_length != NULL) *dest_length = d;

return d;
}

BOOL is_even(int number)
{
return number % 2 == 0;
}

BOOL is_odd(int number)
{
return !is_even(number);
}

BOOL is_anything(int number)
{
return 1;
}

int main()
{
const int sz = 6;
int array[sz] = {63, 12, 10, 11, 8, 77};
int even[sz], ez;
int odd[sz], oz;

copy_if(even, &ez, array, sz, is_even);

copy_if(odd, &oz, array, sz, is_odd);

copy_if(array, NULL, even, ez, is_anything);

/* we know &array[ez] is the next free space */

copy_if(&array[ez], NULL, odd, oz, is_anything);

for(int i = 0; i < sz; ++i)
printf("%d ", array[i]);

return 0;
}```

3. Originally posted by deedlit
Is it possible to add something to this program to make it so the latter half, the odd numbers, are also in the same order they began in at the end of the array?

Ex:

before: 1428754744
after: 4284441757

Thanks
Yes, it's very possible. Think about it.

4. Thanks again.

5. correct me if I'm mistaken, but qsort isn't a stable sort, and [he|she]'s asking for a stable sort, so another sorting algorithm is required.