C 144 163
#define f float
f T, *C, m;
f *q(f *A, f *B, int S, f s)
{
if(m)
return abs(T - *A) - abs(T - *B);
for (
C = malloc(S * 4);
m = S--;
C[S] = *B
)
T = A[S],
qsort(B, s, 4, q);
return C;
}
Okay... I think this little code needs explanation.
At first i tried to do the job with two level of for loop finding the min difference and set the current value to min of B's value. That's very basic.
The same thing can be reached with qsort and a comparator function. I make it sort B by the difference instead of B's elements. Too many functions for such a little algorithm. So the function q now serves two purposes. At first, it's the algorithm itself, secondly (when qsort calls it) a comparator. For communication between the two states, I had to declare globals.
m stands for whether it's in comparator state or the main one.
example:
float A[] = {1.5, 5.6, 8.9, -33.1};
float B[] = {-20.1, 2.2, 10.3};
float *C;
C = q(A, B, sizeof(A)/sizeof(*A), sizeof(B)/sizeof(*B));
// C holds 2.2,2.2,10.3,-20.1