Richard wrote:
) Willem said:
)> Speed wrote:
)> ) Hi,
)> )
)> ) Managed to put together some code. It works fine now but I needed to
)> ) make the Data[] into a global variable. Is there any way of passing
)> ) the Data[] to qsort() along with the Index_Array[]?
)>
)> Not that I know of, I'm afraid. That's one of the major drawbacks of
)> the C qsort() function, actually. The best you can do is have a global
)> pointer to get to the Data[], I think.
)
) Um, why not just sort pointers?
)
) Taking the original example:
)
) struct foo
) {
) double *ptr;
) size_t idx;
) };
Good idea, although that doubles the amount of data that qsort
has to shuffle. But maybe we can do without. How about:
#define N 5
double X[N] = {34.5, 56.4, 12.9, 43.2, 21.7};
double *Pointer_Array[N];
size_t Index_Array[N];
for(i = 0; i < N; i++)
{
Pointer_Array = X + i;
}
qsort(Pointer_Array, N, sizeof Pointer_Array[0], cmpdouble);
And then:
for (i = 0; i < N; i++)
{
Index_Array = Pointer_Array - X;
}
With the obvious compare function:
int cmpdouble(const void *vleft, const void *vright)
{
const double *left = vleft;
const double *right = vright;
return (*left < *right) - (*left > *right);
}
Although that's still a lot of extra overhead just to avoid
a global pointer.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT